pour un macro spécifique à un fichier excel,il s'exécute sans problème ,voilà le code correspondant :


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
private static void callExcelMacro(File file, String macroName) {
ComThread.InitSTA();
 
final ActiveXComponent excel = new ActiveXComponent("Excel.Application");
 
try {
// This will open the excel if the property is set to true
// excel.setProperty("Visible", new Variant(true));
 
final Dispatch workbooks = excel.getProperty("Workbooks")
.toDispatch();
final Dispatch workBook = Dispatch.call(workbooks, "Open",
file.getAbsolutePath()).toDispatch();
 
// Calls the macro
final Variant result = Dispatch.call(excel, "Run",
new Variant(file.getName() + macroName));
 
// 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();
}
 
}
je fais appel à la méthode par ce code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
final File file = new File( "D:\\essay1.xlsm");
final String macroName = "!Module1.Macro2";
callExcelMacro(file, macroName);
mais pour exécuter un macro personnalisé, cela ne marche plus !!!!!!!!
j'ai affecté à macroName toutes les valeurs possibles , mais aucune réponse