3
我正在使用JACOB API从VB宏中调用一些Sub。我想阻止由此宏生成的MsgBox。如何从Java代码中使用Jacob禁用VB6 MsgBox
这是我的代码来打开宏XXXX.xls并运行包含一些MsgBox的子traiteOT。
`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();
//String eventSink = null ;
Dispatch.call(workbooks,"Add");
Dispatch workBook = Dispatch.call(workbooks,"Open", file.getAbsolutePath()).toDispatch();
ExcelEventHandler w = new ExcelEventHandler();
Variant V1=new Variant(file.getName() + macroName);
// Calls the macro
final Variant result = Dispatch.call(excel, "Run", V1);
// 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();
}
}
public static void main(String[] args) {
ExcelMacroTest emt = null;
try {
final File file = new File("D:XXXXXXXX.xls");
final String macroName = "!TraiteOT";
callExcelMacro(file, macroName);
} finally {
if (emt != null) {
emt.quit();
}
}
}
}
`
感谢Philip的回答。但是,我想从VB宏中调用一个子程序而不改变这个宏的代码。那么,您能解释一下使用Visual Basic for Applications Extensibility的解决方案吗? – 2013-03-25 21:37:25
好吧,它基本上意味着你以编程的方式创建你想要执行它的程序,然后执行它...读在[Ozgrid](http://www.ozgrid.com/forum/showthread.php?t=48694 )和[以编程方式添加宏到Excel](http://vbadud.blogspot.com/2008/10/programmatically-add-macro-to-excel.html) – 2013-03-26 09:26:08
此过程已经实现到宏和我希望从中受益。然后,如果它将被运行,它可以显示一个阻止程序运行的msgBox。 – 2013-03-26 11:25:40