如何使用Jacob在Java代码中禁用VB6 MsgBox

5
我正在使用JACOB API调用VB宏中的一些子过程。我想要阻止由该宏生成的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();
            }
        }
    }
}

`

1个回答

1

如果你不注释执行该函数的代码或者以其他方式避免调用该代码,是无法阻止msgbox的弹出的。

如果确实需要这样做,可以将VBA代码读入变量,去除Msgbox函数调用,然后使用Visual Basic For Applications Extensibility执行它。

最好的方法是在工作簿中编写一个没有MsgBox的特殊函数。


感谢Philip的回答。但是,我想在不更改此宏代码的情况下从VB宏中调用子例程。那么,您能否更详细地解释一下如何使用Visual Basic For Applications Extensibility解决方案? - khaled Rihane
好的,基本上意味着您可以以编程方式创建要执行的过程,然后执行它...请阅读 Ozgridprogrammatically-add-macro-to-excel - Our Man in Bananas
这个过程已经实现为宏,我希望从中受益。然后,如果运行它,它可以显示一个 msgBox,阻止该过程的运行。 - khaled Rihane
没有简单的方法可以阻止Messagebox函数。你需要复制所有代码并删除包含Msgbox函数调用的部分。 - Our Man in Bananas
我能否从Java(Jacob)访问宏代码并注释包含此MsgBox的行? - khaled Rihane
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接