简体   繁体   English

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

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

I have an application that at this point calls a VBScript file with ~11 parameters. 我有一个应用程序,该应用程序此时使用〜11个参数调用VBScript文件。 Those parameters need to be caught and saved. 这些参数需要捕获并保存。 Then a Word template document needs to be called and filled in. What would be the best way to do this? 然后需要调用并填写Word模板文档。什么是最好的方法? I'm searching for a solution, but apparently VBScript does not support the same assignments as VBA so I'm walking against a wall. 我正在寻找解决方案,但是显然VBScript不支持与VBA相同的分配,所以我要靠墙走。

Showing the arguments: 显示参数:

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

Opening Word: 开场词:

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

Solved the problem with the following script. 使用以下脚本解决了该问题。

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

Script receives arguments, saves what kind of Tags have been used within the Word document (I have used Rich Text Content Control), checks if the Word is open and if not opens it, grabs the needed document and finally fills the Content Control with the received arguments. 脚本接收参数,保存在Word文档中使用了哪种标记(我使用过RTF内容控件),检查Word是否打开,如果没有打开,则获取所需的文档,最后用收到论点。 Tested with Word 2013. I'm probably gonna change the script but this could be a guide line for someone else. 已通过Word 2013测试。我可能会更改脚本,但这可能是其他人的指南。

Vague answer to a vague question, but generally speaking: 对于一个模糊的问题,答案很模糊,但总的来说:

Use oWord instead of Application : 使用oWord代替Application

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

Use literals instead of wd______ constants: 使用文字而不是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