[英]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.