如何使用Jacob从java代码中禁用VB6 MsgBox

时间:2013-03-24 07:17:43

标签: java excel-vba jacob vba excel

我正在使用JACOB API从VB宏调用一些Sub。我想阻止这个宏生成的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 个答案:

答案 0 :(得分:1)

除非您对执行函数的代码进行注释,否则无法阻止msgbox,避免以其他方式调用该代码。

如果你真的不得不,你可以将VBA代码读入一个变量,去掉Msgbox函数调用,然后执行它(使用Visual Basic For Applications Extensibility)

最好只编写一个没有工作簿中的MsgBox的特殊函数