2013-06-26 49 views
0

我在通過VBScript在InfoPath中執行保存和關閉操作時遇到困難。如何使用VBS保存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

XDocumentsXDocument對象的集合。它沒有Save方法,並且Close方法的參數是要關閉的對象的索引,而不是布爾值。如果您想保存並關閉一個特定的對象,你可能需要做這樣的:

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

未經檢驗的,但因爲我沒有手頭有足夠新的MS Office。

+0

感謝您回答@Ansgar。你和代碼非常接近,我玩了大約30分鐘,最後發現需要一些額外的引號與fileName參數串聯起來。我不知道爲什麼,但你需要那些額外的引號。這裏是更正的段:'Set xdoc = InfoPathObject.XDocuments.Open(fileName +「」)xdoc.Save xdoc.CloseDocument' – Kyle