1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public static boolean launchMacro(final File file, final String macroName) throws FrameWorkException {
ActiveXComponent excel = null;
boolean macroLaunched = false;
try {
excel = new ActiveXComponent("Excel.Application");
excel.setProperty("Visible", new Variant(true));
excel.setProperty("EnableEvents", new Variant(false));
// Dispatch addins = excel.getProperty("AddIns").toDispatch();
// Dispatch.call(addins, "Add", "P:\\MacroRED.xla", false);
Dispatch workbooks = excel.getProperty("WorkBooks").toDispatch();
@SuppressWarnings("unused")
Dispatch workbook = Dispatch.call(workbooks, "Open", new Object[] { file.getAbsolutePath() }).toDispatch();
Dispatch addins = excel.getProperty("AddIns").toDispatch();
Variant xla = Dispatch.call(addins, "Add", "P:\\MacroRED.xla", false);
Dispatch.put(xla.toDispatch(), "Installed", true);
// String macroFullName = file.getName() + "!" + macroName;
String macroFullName = macroName;
@SuppressWarnings("unused")
Variant result = Dispatch.call(excel, "Run", new Variant(macroFullName));
Dispatch.call(workbook, "Save");
macroLaunched = true;
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
} catch (ComFailException cfe) {
// Traduction de l'excetion JACOB
String msg = null;
try {
msg = cfe.getMessage().subSequence(
cfe.getMessage().indexOf(JABOC_COM_EXCEPTION_KEY_WORD) + JABOC_COM_EXCEPTION_KEY_WORD.length(),
cfe.getMessage().length()).toString();
} catch (Throwable t) {
msg = "Unknown error";
}
if ("".equals(msg.trim())) {
msg = "Unknown error";
}
throw new FrameWorkException(MACRO_ERROR, new Object[] { macroName, msg });
} finally {
excel.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return macroLaunched;
} |
Partager