Processing MS Word with VBS on Windows

The general question is: how can you "translate" functionality in VBA macros created by "recording" in Microsoft Office into VBScript executable files, which can be in .vbs files?

Specific question: how can a package create thumbnails of Word documents for viewing in Windows Explorer?

Alternative question: where can I find documentation on manipulating MS Word documents using VBS?


My ultimate goal is the batch process of creating thumbnails for MS Word documents.

my human method:

  • open word document
  • click save as
  • mark "save sketch"
  • save and replace

I learned from a small website that VBS, in the form of .vbs files, can manipulate Word documents. An example that can be executed by double-clicking the .vbs file in Windows Explorer:

'in a file called "something.vbs"
Set objWord = CreateObject("Word.Application")

objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "Network Adapter Report"
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()

I also found out that by “recording macros” I can get VBA code that saves a document with a thumbnail. here is the macro i wrote:

Sub save_with_thumbnail()
'
' save_with_thumbnail Macro
'
'
    ChangeFileOpenDirectory _
        "E:\"
    ActiveDocument.SaveAs2 FileName:="as90520.doc", FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0
End Sub

each of the two approaches solves part of my problems, but I could not integrate them. so I ask if there is anyone -

  • can help integrate / merge / any of the two approaches by converting what the VBA macro did into an executable script, or
  • can give a suggestion on how to make thumbnails for MS Word documents in Windows Explorer or
  • aware of the existence of any reference documentation somewhere that provides additional information about this CreateObject.("Word.Application")and .Documents.Add()and .Selectionand ..SaveAs- all sorts.
I hope I have formulated the question well enough. Thanks in advance for any help.
+3
source share
3 answers

The starting point is different. VBA is usually hosted in another application that provides a set of built-in objects; for example, if your VBA is hosted in Word, you will have access to Word Application, which refers to the current Word application. In VBS, you must either create a new Application object and save it in a variable:

Dim wdApp
Set wdApp = CreateObject("Word.Application")

or get a link to an already running Word application:

Set wdApp = GetObject(,"Word.Application")

:

Dim wdDoc
Set wdDoc = wdApp.Open("path\to\document.docx")


, VBA . (Dim wdDoc) VBA :

Dim wdDoc As Word.Document
'alternatively:
'Dim wdDoc As Document

, VBA , wdFormatDocument. VBScript :

Const wdFormatDocument = 0

:

wdApp.ActiveDocument.SaveAs2 FileName:="as90520.doc", FileFormat:= 0


Word .
, ActiveDocument Application (. ). VBS :

Dim wdApp
Set wdApp = CreateObject("Word.Application")

'When you open Word from the Start menu, it automatically adds a blank document for you
'When manipulating Word in a program, we need to do this by hand
'Generally we would store this in a variable, but we don't need to store it in order
'to use the ActiveDocument property; it just has to exist
wdApp.Documents.Add

'copied and pasted from before
wdApp.ActiveDocument.SaveAs2 FileName:="as90520.doc", FileFormat:= _
    wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    SaveAsAOCELetter:=False, CompatibilityMode:=0
+6

.

VBA, , " ". - . , , " " " " .

" VBA VBScript" , VBA . Microsoft, , : http://msdn.microsoft.com/en-us/library/office/ff836084(v=office.14).aspx.

" " .

+1

Registry

There is a registry setting that allows you to save thumbnails in "HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Office \ 16.0 \ Common" of the DWORD type with the name SaveThumbnails (1 for saving, 0 for NOT save).

0
source

All Articles