繁体   English   中英

VBA / VBScript到Word模板文档(.docm / .dotx)

[英]VBA/VBScript to Word Template document (.docm/.dotx)

我有一个应用程序,该应用程序此时使用〜11个参数调用VBScript文件。 这些参数需要捕获并保存。 然后需要调用并填写Word模板文档。什么是最好的方法? 我正在寻找解决方案,但是显然VBScript不支持与VBA相同的分配,所以我要靠墙走。

显示参数:

Set args = WScript.Arguments
For I = 0 to args.Count -1
    Wscript.Echo args(I)
Next

开场词:

Set oWord = CreateObject("Word.Application")
oWord.Visible = True

使用以下脚本解决了该问题。

Set args = WScript.Arguments
Dim wordIds
    wordIds = Array("idDate", "idName", "idCompany")
Dim oWord
Dim oDoc
Dim sDocPath

' If the next line gives an error continue with the line that comes next '
On Error Resume Next
    ' Look if a Word object is already open '
    Set oApp = GetObject(, "Word.Application")

' If number of errors is higher or lower than 0 create a new Word object '
If Err.Number <> 0 Then
    Set oApp = CreateObject("Word.Application")
End If

' Specify the Word document and open it for the user '
sDocPath = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\template.dotm"
Set oDoc = oApp.Documents.Open(sDocPath)
oApp.Visible = True

' Iterate through all arguments and fill them in the ContentControls'
for I = 0 to args.Count -1
    oDoc.SelectContentControlsByTag(wordIds(I))(1).Range.Text = args(I)
Next

脚本接收参数,保存在Word文档中使用了哪种标记(我使用过RTF内容控件),检查Word是否打开,如果没有打开,则获取所需的文档,最后用收到论点。 已通过Word 2013测试。我可能会更改脚本,但这可能是其他人的指南。

对于一个模糊的问题,答案很模糊,但总的来说:

使用oWord代替Application

oWord.CheckGrammar(someString) '// Instead of Application.CheckGrammar()

使用文字而不是wd______常量:

myDoc.SaveAs2("SomeDoc.docx", 12) '// Instead of myDoc.SaveAs2("SomeDoc.docx", wdFormatXMLDocument)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM