如何使用VBS保存InfoPath文档?

时间:2013-06-26 21:49:59

标签: vbscript infopath

我无法通过VBScript在InfoPath中执行保存和关闭操作。

我正在尝试执行一些简单的操作,因此我可以稍后介绍更改。我已经检查MSDN以确保我的语法正确...而且似乎......但是在SAVE和CLOSE操作期间脚本崩溃了。

有没有VBS Guru可以告诉我我做错了什么?

这里的逻辑是非常基本的。 1.提供一个InfoPath文件列表供我使用。 2.逐行循环遍历文件,然后执行打开/保存/关闭操作。 3.退出程序。

以下是代码:

'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()

编辑:将已接受的答案纳入脚本

1 个答案:

答案 0 :(得分:1)

XDocumentsXDocument个对象的集合。它没有Save方法,Close方法的参数是要关闭的对象的索引,而不是布尔值。如果你想保存并关闭一个特定的对象,你可能必须这样做:

Set xdoc = InfoPathObject.XDocuments.Open(fileName)
xdoc.Save
xdoc.CloseDocument

未经测试,因为我手边没有足够的MS Office。