如何使用Jacob从Java运行Excel个人VBA宏
如果宏位于Excel文件中,则此代码将运行宏。它不再运行个人宏,因为知道该宏可以手动正常工作如何使用Jacob从Java运行Excel个人VBA宏,java,excel,vba,jacob,Java,Excel,Vba,Jacob,如果宏位于Excel文件中,则此代码将运行宏。它不再运行个人宏,因为知道该宏可以手动正常工作 private static void callExcelMacro(File file, String macroName) { ComThread.InitSTA(); final ActiveXComponent excel = new ActiveXComponent("Excel.Application"); try { // This
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();
}
}
我调用此函数来运行Excel文件中的宏
final File file = new File( "D:\\essay1.xlsm");
final String macroName = "!Module1.Macro2";
callExcelMacro(file, macroName);
如何运行个人宏?忽略模块名称-不需要这样做