如何将多个参数从Java传递到.vbs文件

时间:2016-09-19 17:32:38

标签: java excel-vba vbscript vba excel

我编写了一个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();
           }

    }

2 个答案:

答案 0 :(得分:1)

使用ProcessBuilder代替.exec

Process p = new ProcessBuilder("wscript", scriptpath, destinationlocation).start();

答案 1 :(得分:0)

谢谢大家的回复,我的问题解决了我在wscript中添加“”并在myVB2.vbs中删除了filepath中的set