0

我在通过 VBScript 在 InfoPath 中执行保存和关闭操作时遇到了困难。

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

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

这里的逻辑非常基本。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()

编辑:将接受的答案合并到脚本中

4

1 回答 1

1

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

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

不过,未经测试,因为我手头没有足够新的 MS Office。

于 2013-06-26T22:10:34.947 回答