I am having difficulty performing Save and Close operations in InfoPath through a VBScript.
I'm trying to just perform some simple operations, so I can introduce changes later. I've checked MSDN to make sure my syntax is correct...and it seems to be...yet the script crashes during the SAVE and CLOSE operations.
Are there any VBS Guru's out there who can show me what I'm doing wrong?
The logic here is very basic. 1. Provide a list of InfoPath files for me to use. 2. Loop through the file line by line, and perform the open/save/close operation. 3. Quit the program.
Here's the code:
'This line gets the current working directory, based off string subtraction from the absolute path of the script - the script's name
workingDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(Len(WScript.ScriptName)))
'We need to provide a name for the file we're going to read. It's nice to keep this in one place, and not let it change.
Const XMLList = "XML Targets.txt"
'Create a file system object so we can open the list of XML files that you want opened
Set fso = CreateObject("Scripting.FileSystemObject")
'General Error checking, Just making sure there's a XML List file to read from.
If fso.FileExists(workingDirectory & XMLList) Then
Set targetFile = fso.OpenTextFile(workingDirectory & XMLList)
Else
MsgBox "You need to have a file called '" & XMLList & "' in the same directory as the script!", 16, "Error"
WScript.quit()
End If
'Assuming you get this far, you loop through the list file line by line and open/save/close the documents
Do While Not targetFile.AtEndOfStream
'Create an InfoPath Application object so we can manipulate our files in InfoPath
Set InfoPathObject = CreateObject("InfoPath.Application")
fileName = targetFile.ReadLine()
Set xdoc = InfoPathObject.XDocuments.Open(fileName+"") 'Open the file (Suggested by Ansgar)
xdoc.Save 'Save the file (Suggested by Ansgar)
xdoc.CloseDocument 'Close the file (Suggested by Ansgar)
Loop
MsgBox "Done"
WScript.quit()
Edit: Incorporated accepted answer into script
XDocuments
is a collection of XDocument
objects. It doesn't have a Save
method, and the parameter of the Close
method is the index of the object to be closed, not a boolean value. If you want to save and close a particular object, you'll probably have to do it like this:
Set xdoc = InfoPathObject.XDocuments.Open(fileName)
xdoc.Save
xdoc.CloseDocument
Untested, though, because I don't have a recent enough MS Office at hand.