我想使用vbs文件作爲橋樑,以便從Java訪問Excel文件中的vba-macro。另外,我需要傳輸一些可變參數。VBS:在application.run中使用變量時發生類型衝突
在Java中我打電話打開VBS文件這樣的(正常工作):
String strFileName = "mappe2.xlsm"; //located in the same directory
String strMacroName = "showname";
String strParameter = "Myname";
String[] command = {"wscript.exe", f.getAbsolutePath(), strFileName, strMacroName, strParameter};
Process p = Runtime.getRuntime().exec(command);
在Excel中我只是嘗試調用這個簡單的子:
Sub showname(strName As String)
MsgBox ("The name is: " & strName)
End Sub
我的(縮短) vbs文件看起來像這樣:
Dim args, ExcelFileName, ExcelMacroName
Dim opt
Dim Command2
Set args = WScript.Arguments
ExcelFileName = args(0)
ExcelMacroName = args(1)
opt = Cstr(args(2))
Set objWbk = GetObject("E:\Mappe2.xlsm")
Command2 = ExcelFileName & "!" & "showname"
'objWbk.Application.Run Command2, "opt" 'This way it works!
objWbk.Application.Run Command2, opt 'Error: "Type conflict" occurs!
WScript.Quit
而在這裏你可以看到,與靜態「選擇」的宏調用工作正常。但是,由於我將其更改爲變量,我得到了類型衝突的錯誤,我不知道爲什麼。我需要讓它變化。
你能幫我嗎?
這只是一個瘋狂的猜測,但嘗試調用'objWbk.Application.Run Command2,Eval(opt)' – 2013-07-08 12:43:43
Thanky你爲這個嘗試!至少宏啓動了,但參數傳輸不正確。所以opt是空的,Msgbox顯示如下:「名字是:」 – Eleandro
你確定args(2)不是null嗎? – wingyip