I have a Excel VBA written and trying to call it from java (using Jacob) as below:
public void PnlCubeExcelMacroCallAndRefresh() {
ComThread.InitSTA();
File file=new File("C:\\Users\\Pnl_Cube_Template.xlsm");
// String macroName="Retrieve()";
String macroName="Retrieve()";
final ActiveXComponent excel = new ActiveXComponent("Excel.Application");
try {
// This will open the excel if the property is set to true
excel.setProperty("Visible", new Variant(true));
final Dispatch workbooks = excel.getProperty("Workbooks").getDispatch();
//String eventSink = null ;
int id = Dispatch.get(workbooks, "Count").getInt();
System.out.println("le nbre" + id);
Dispatch.call(workbooks, "Add");
Dispatch workBook = Dispatch.call(workbooks, "Open", file.getAbsolutePath()).toDispatch();
Variant V1 = new Variant( file.getName() + macroName);
// Calls the macro
final Variant result = Dispatch.call(excel, "Run", V1);
// Saves and closes
//Dispatch.call(workBook, "Save");
com.jacob.com.Variant f = new com.jacob.com.Variant(true);
// Dispatch.call(workBook, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
excel.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}
But when I run this below error is encountered:
com.jacob.com.ComFailException: Invoke of: Run Source: Microsoft Excel Description: Cannot run the macro 'Pnl_Cube_Template.xlsmRetrieve'. The macro may not be available in this workbook or all macros may be disabled.
I have tried changing the trust center settings as well, still same issue.