0

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();
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Alann
  • 647
  • 5
  • 19
  • Reconfigure the task to run under your user account, its currently running under another account that cannot access your user directory. (Or use a path that is available to anyone) – Alex K. Jun 29 '16 at 10:17
  • @AlexK. It's already configure to do it this way, if not the jar wouldn't start – Alann Jun 29 '16 at 10:20

0 Answers0