Reputation: 934
I have an application that at this point calls a VBScript file with ~11 parameters. 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? I'm searching for a solution, but apparently VBScript does not support the same assignments as VBA so I'm walking against a wall.
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
Upvotes: 1
Views: 1400
Reputation: 934
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. Tested with Word 2013. I'm probably gonna change the script but this could be a guide line for someone else.
Upvotes: 1
Reputation: 17647
Vague answer to a vague question, but generally speaking:
Use oWord
instead of Application
:
oWord.CheckGrammar(someString) '// Instead of Application.CheckGrammar()
Use literals instead of wd______
constants:
myDoc.SaveAs2("SomeDoc.docx", 12) '// Instead of myDoc.SaveAs2("SomeDoc.docx", wdFormatXMLDocument)
Upvotes: 0