我写了一个VB脚本(myVB2.vbs),它基本上在excel中运行宏并保存excel。
这是我的VB脚本(myVB2.vbs)
Dim objExcel, objWorkbook, filePath
Set filePath = Wscript.Arguments(0)
Set objExcel = CreateObject("Excel.Application")
'Set objWorkbook = objExcel.Workbooks.Open("C:\Writter\Excel.xlsm")
Set objWorkbook = objExcel.Workbooks.Open(filePath)
objExcel.Visible = True
objExcel.Run "ChartUpdate"
objWorkbook.Save
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Quit
现在,我需要通过将excel文件路径的参数传递给VBscript从Java触发此.vbs文件。使用下面的Java代码,我无法传递文件路径的参数。出现错误-文件扩展名“ .xlsm”没有脚本引擎。
public static void main(String[] args) throws IOException {
String scriptpath = System.getProperty("user.dir") + "\\src\\com\\domain\\function\\resources\\myVB2.vbs";
String destinationlocation ="C:\\Writter\\Excel.xlsm" ;
Runtime rt = Runtime.getRuntime();
try {
Runtime.getRuntime().exec( "wscript"+ scriptpath+" "+destinationlocation);
}
catch( IOException e ) {
e.printStackTrace();
}
}
最佳答案
使用ProcessBuilder
代替.exec
:
Process p = new ProcessBuilder("wscript", scriptpath, destinationlocation).start();