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

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

问题描述

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

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?

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

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

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

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

我的最终目标是批处理为 MS Word 文档创建缩略图的过程.

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

我的人为方法是:

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

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

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

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

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 -

  • 可以通过将 VBA 宏执行的操作转换为 Windows 可执行脚本来帮助集成/组合/两种方法,
  • 可以就如何在 Windows 资源管理器中为 MS Word 文档批量创建缩略图提供建议
  • 知道某处存在一些提供有关此 CreateObject.("Word.Application").Documents.Add()CreateObject.("Word.Application") 的更多信息的参考文档code>.Selection 和 ..SaveAs - 无论如何,各种.
  • 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.

推荐答案

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

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")

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

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	odocument.docx")

<小时>请记住,在 VBA 中变量可以有一个类型.与之前的变量声明(Dim wdDoc)不同,在 VBA 中您可以看到:


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

此外,VBA 通常可以访问枚举常量,例如 wdFormatDocument.在 VBScript 中,您可以手动定义常量:

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

<小时>可以在此处找到 Word 对象模型的参考.<小时>就您的具体问题而言,ActiveDocumentApplication 对象的属性,(请参阅 此处).所以在 VBS 中,对应的代码可能是这样的:


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天全站免登陆