3

I have a macro defined in an Excel file and I want invoke it from a Java program using Jacob 1.18 jar and dll.

The following is the code segment I am currently using.

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TestJacob {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        File file = new File("C:\\TestJacob\\TestExcel_copy.xlsm");
        String macroName = "TestMacro";
        callExcelMacro(file, macroName);

    }

    private static void callExcelMacro(File file, String macroName) {
        ComThread.InitSTA(true);
        final ActiveXComponent excel = new ActiveXComponent("Excel.Application");
        try{
            excel.setProperty("EnableEvents", new Variant(false));

            Dispatch workbooks = excel.getProperty("Workbooks")
                    .toDispatch();

            Dispatch workBook = Dispatch.call(workbooks, "Open",
                    file.getAbsolutePath()).toDispatch();

            // Calls the macro
            Variant V1 = new Variant( file.getName() + macroName);
            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();
        }
    }
}

The following is the exception I am getting while calling the macro .

com.jacob.com.ComFailException: Invoke of: Run
Source: Microsoft Excel
Description: Cannot run the macro 'TestExcel_copy.xlsmTestMacro'. The macro may not be available in this workbook or all macros may be disabled.

    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
    at com.jacob.com.Dispatch.callN(Dispatch.java:453)
    at com.jacob.com.Dispatch.call(Dispatch.java:541)
    at TestJacob.callExcelMacro(TestJacob.java:38)
    at TestJacob.main(TestJacob.java:16)

I have also enabled the macros in Excel file by following steps .

  1. File->Options
  2. Trust Center-> Trust Center Settings
  3. Macro Settings
  4. Enabled "Enable all macros" and "trust access to the VBA project object model
  5. press "OK"
Community
  • 1
  • 1

3 Answers3

3

u need to change your macro call to

Variant result = Dispatch.call(excel, "Run", new Variant("\'"+file.getName()+"\'"+ macroName));

because inside excel the name of the file is between quote, so it doesn't find your macro name without "\'"

Alann
  • 647
  • 5
  • 19
1

To be exact :

Dispatch.call(excel, "Run", new Variant("\'"+file.getName()+"\'!" + macroName));
Jaap
  • 81,064
  • 34
  • 182
  • 193
0

No need to add file.getname() while calling the macro. Only the macroname is sufficient.

Use

Variant result = Dispatch.call(excel, "Run", new Variant(macroName));