Vinmee Vinmee - 2 months ago 35
Java Question

How to Pass Multiple parameter to .vbs file from Java

I have a VB Script (myVB2.vbs) written, which basically runs the macro in excel and saves the excel.
Here is my VB script(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


Now I will need to trigger this .vbs file from Java by passing argument of the excel file path to VBscript. With below piece of Java code, I am not able to Pass argument of the file path.I get error - There is no script engine for file extension ".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();
}

}

Answer

Use ProcessBuilder instead of .exec:

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