handling MS Word with VBS on Windows
Asked Answered
C

3

3

General question: how can functionality in VBA macros created by 'recording' in Microsoft Office be 'translated' into Windows-executable VBScripts that could be in .vbs files?

Specific question: how to batch create thumbnails of Word documents for viewing in Windows Explorer?

Alternative question: where can i find documentation on manipulating MS Word documents with VBS?


my final goal is to batch the process of creating thumbnails for MS Word docs.

my humanly-done method is:

  • open a Word doc
  • press 'save as'
  • tick 'save thumbnail'
  • save and replace

i found out from a small website that VBS, in the form of .vbs files, can manipulate Word documents. 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 some VBA code that saves a document with a thumbnail. here is a macro that i recorded:

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 solve part of my problems, but i couldn't integrate them two. therefore i'm asking if anyone -

  • can help integrate/combine/whatever the two approaches by converting what the VBA macro did into a windows-executable script, or
  • can give a suggestion on how to batch create thumbnail for MS Word docs in Windows Explorer, or
  • know of the existence of some reference documentation somewhere that provides more information about this CreateObject.("Word.Application") and .Documents.Add() and .Selection and ..SaveAs - whatever, all sorts.

hope i have worded the question well enough. thanks in advance for any help.

Cockrell answered 18/11, 2012 at 23:3 Comment(1)
What version of Office/Windows do you have? According to Wikipedia, there should be no need to export Office documents as images in order to create thumbnails.Asepsis
A
6

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

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

or get a reference to an already running Word Application:

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

Once you've done that, the code between them is virtually interchangeable:

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


Bear in mind that in VBA variables can have a type. Instead of the previous variable declaration (Dim wdDoc), in VBA you can see this:
Dim wdDoc As Word.Document
'alternatively:
'Dim wdDoc As Document

Also, VBA generally has access to enum constants, such as wdFormatDocument. In VBScript, you can either define the constants manually:

Const wdFormatDocument = 0

Or use the value of the constants directly:

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


Reference for the Word object model can be found here.
As far as your specific question goes, ActiveDocument is a property of an Application object, (see here). So in VBS, the corresponding code might look something like this:
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
Asepsis answered 19/11, 2012 at 0:12 Comment(2)
greatly appreciating your rapid & thorough answer! i haven't tried it yet, but i'm about to try it now and i believe it works.Cockrell
I've just picked up the problem again. Nothing works. Not even Microsoft seems to care about thumbnails; no documentation even mentions the word "thumbnail". The recorded macros, both when I ticked "save thumbnail" and not, are the same. VBA doesn't seem capable of doing the task, not to mention "trying to adapt VBA code to VBS". I'm doubting if it is Word that takes care of the "thumbnail" at all.Cockrell
C
1

This answer is not intended to be a complete answer.

That VBA code created by recording a macro does not solve "part of the question" at all. It doesn't solve any part of the question. I experimented and found out that the macro would be the same regardless of whether the "save thumbnail" checkbox in the "save as" dialog is checked or not.

The idea of "adapting VBA code to VBScript" wouldn't work, because VBA itself doesn't even work. Not even Microsoft seems to care about thumbnails: http://msdn.microsoft.com/en-us/library/office/ff836084(v=office.14).aspx .

The requirement in the "specific question" could not be met in the asker's intended approach.

Cockrell answered 3/12, 2012 at 8:59 Comment(1)
An answer of "It's impossible" is also an answer.Asepsis
A
0

Registry

There is a registry setting to enable saving thhumbnails at "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Common" of type DWORD named SaveThumbnails (1 to save; 0 to NOT save).

Auditor answered 13/3, 2017 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.