Bonjour,
Sous excel pour lancer une macro incluse dans le fichier excel j'utilise le code ci-dessous. Cependant j'aimerai exécuter des macros qui ne se situent pas dans le fichier excel mais dans un fichier .xla définissant des macros supplémentaire.
Savez-vous comment je peux faire ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51 /** * Launch a macro in an excel file * * @param file * the excel file * @param macroName * the name of the macro label like this 'Sheet1.Macro1' * @throws FrameWorkException * en cas d'erreur */ 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(false)); excel.setProperty("EnableEvents", new Variant(false)); Dispatch workbooks = excel.getProperty("WorkBooks").toDispatch(); @SuppressWarnings("unused") Dispatch workbook = Dispatch.call(workbooks, "Open", new Object[] { file.getAbsolutePath() }).toDispatch(); String macroFullName = file.getName() + "!" + 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