在Windows上使用VBS处理MS Word [英] handling MS Word with VBS on Windows

查看:318
本文介绍了在Windows上使用VBS处理MS Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般问题: 通过Microsoft Office中的记录"创建的VBA宏中的功能如何被翻译"为可以在.vbs文件中的Windows执行的VBScript中?

具体问题: 如何批量创建Word文档的缩略图以在Windows资源管理器中查看?

替代问题: 在哪里可以找到有关使用VBS处理MS Word文档的文档?


我的最终目标是分批为MS Word文档创建缩略图.

我的人为方法是:

  • 打开Word文档
  • 按另存为"
  • 勾选保存缩略图"
  • 保存并替换

我从一个小型网站上找到了 VBS以.vbs文件的形式可以操纵Word文档.可以通过双击Windows资源管理器中的.vbs文件来执行的示例:

'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()

我还发现,通过记录宏",我可以获得一些VBA代码,该文件可以保存带有缩略图的文档.这是我录制的宏:

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

这两种方法中的每一种都可以解决我的问题的 part ,但是我无法将它们两者结合在一起.因此,我要问是否有人-

  • 通过将VBA宏的内容转换为Windows可执行脚本
  • ,可以帮助集成/组合/这两种方法中的任何一种
  • 可以提供有关如何在Windows资源管理器中批量创建MS Word文档缩略图的建议
  • 知道某处存在一些参考文档,这些文档提供了有关CreateObject.("Word.Application").Documents.Add().Selection..SaveAs的更多信息-各种形式.
希望我对这个问题的措辞足够好.预先感谢您的帮助.

解决方案

初始起点不同. VBA通常由另一个应用程序托管,该应用程序提供了一组内置对象.例如,如果您的VBA托管在Word中,则您可以访问Word Application,它引用了当前正在运行的Word应用程序.在VBS中,您必须创建一个新的Application对象并将其保存在变量中:

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

或获取对已经运行的Word应用程序的引用:

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

完成此操作后,它们之间的代码几乎可以互换:

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


请记住,在VBA中,变量可以具有类型.在VBA中,您可以看到以下内容,而不是以前的变量声明(Dim wdDoc):

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对象模型的参考.
就您的特定问题而言,ActiveDocumentApplication对象的属性,(请参阅

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.

解决方案

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

这篇关于在Windows上使用VBS处理MS Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆