Skip to main content

com4j excel macro execution

1 reply [Last post]
manavilai
Offline
Joined: 2012-08-29
Points: 0

Hi Team,
I am new to using com4j.
I am interested to use it against excel.
I would like to call a excel macro from a Java Program.
I enhanced the ExcelDemo class.

_Workbook wb = app.getWorkbooks().open("C:/vivek/work/CAST/CAST_Macro.xls",
0,
false,
5,
"",
"",
true,
true,
MISSING,
false,
false,
MISSING,
MISSING,
MISSING,
MISSING,
0);
// app.getActiveCell().setFormulaR1C1("Welcome to com4j");
Variant macroName = new Variant(Variant.Type.VT_BSTR);
macroName.set("startup");
Variant saveBeforeExit = new Variant(Variant.Type.VT_BOOL);
saveBeforeExit.set(1);

wb.getApplication().run(macroName,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING,
MISSING);
wb.close(saveBeforeExit, MISSING,MISSING, 0);
app.quit();

on execution of this it gives me an error.any help to resolve this issue.

Exception in thread "main" com4j.ComException: 8002000e Invalid number of parameters. : Invalid number of parameters. : .\invoke.cpp:517
at com4j.Wrapper.invoke(Wrapper.java:166)
at $Proxy5.run(Unknown Source)
at ExcelDemo.main(ExcelDemo.java:39)
Caused by: com4j.ComException: 8002000e Invalid number of parameters. : Invalid number of parameters. : .\invoke.cpp:517
at com4j.Native.invoke(Native Method)
at com4j.StandardComMethod.invoke(StandardComMethod.java:42)
at com4j.Wrapper$InvocationThunk.call(Wrapper.java:340)
at com4j.Task.invoke(Task.java:51)
at com4j.ComThread.run0(ComThread.java:153)
at com4j.ComThread.run(ComThread.java:134)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
manavilai
Offline
Joined: 2012-08-29
Points: 0

i got it to work by changing the code like this.

Variant macro = new Variant(Variant.Type.VT_BSTR);
macro.set("startup");

wb.application().run(macro, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null);