Vinmee Vinmee - 1 year ago 219
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"

Set objWorkbook = Nothing
Set objExcel = Nothing


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 ) {


Answer Source

Use ProcessBuilder instead of .exec:

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