I've create a Jar file which have to throw a VBA Excel macro. To do so I've use the jacob API my code for the call is :
final ActiveXComponent excel = new ActiveXComponent("Excel.Application");
try {
final Dispatch workbooks = excel.getProperty("Workbooks").toDispatch();
final Dispatch workBook = Dispatch.call(workbooks, "Open", file.getAbsolutePath()).toDispatch();
final Variant result = Dispatch.call(excel, "Run", new Variant("\'" + file.getName() + "\'" + macro));
com.jacob.com.Variant f = new com.jacob.com.Variant(true);
Dispatch.call(workBook, "Close", f);
} catch (Exception e) {
ReadExcel.errorLog(e.getMessage());
} finally {
excel.invoke("Quit", new Variant[0]);
ComThread.Release();
}
this code work fine when I launch my jar manually, but on a scheduled task I've got this error :
ERROR - Invoke of: Open Source: Microsoft Office Excel Description: Microsoft Office Excel cannot access the file « C:\Users\Me\Desktop\Project\ExcelWithMacro.xlsm ». There are several possible reasons:
- The file name or path does not exist.
- The file is being used by another program.
- The workbook you are trying to save has the same name as a currently open workbook.
But the file exist, is not use by an other program and is not open ...
the error is throw on the line:
final Dispatch workBook = Dispatch.call(workbooks, "Open", file.getAbsolutePath()).toDispatch();