This question has been asked before Here but it never received any answers.
If I save a macro to a specific (Non Personal Macro) workbook, then I am able to get the macro to run.
I am unable to get a Personal Macro to work after opening up an Excel file. It gives me the error.
com.jacob.com.ComFailException: Invoke of: Run
Source: Microsoft Excel
Description: Sorry, we couldn't find C:\Users\Me\Documents\!PERSONAL.XLSB. Is it possible it was moved, renamed or deleted?
Logic suggests that the program is looking for my Macro at the file path declared in the error, but I'm a little confused as to why it's checking that file path in the first place. Furthermore, if it is checking the wrong file path, how do I get it to direct towards the right one.
My Code:
public static void main(String[] args) throws IOException{
final File file = new File( "path");
final String macroName = "!PERSONAL.XLSB!Macro";
callExcelMacro(file, macroName);
}
private static void callExcelMacro(File file, String macroName) {
ComThread.InitSTA();
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")
.toDispatch();
final Dispatch workBook = Dispatch.call(workbooks, "Open",
file.getAbsolutePath()).toDispatch();
// Calls the macro
final Variant result = Dispatch.call(excel, "Run", macroName);
// 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();
}
}