2013-03-24 60 views
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(); 
      } 
     } 
    } 
} 

`

回答

1

,除非你評論說,執行功能或代碼,你不能阻止MSGBOX,避免調用一些其他的方式代碼。

如果你真正到了,你能讀懂VBA代碼到一個變量,去掉MsgBox函數調用,然後執行它(使用Visual Basic應用程序擴展)

最好只寫一個特殊功能,而不工作簿中的MsgBox

+0

感謝Philip的回答。但是,我想從VB宏中調用一個子程序而不改變這個宏的代碼。那麼,您能解釋一下使用Visual Basic for Applications Extensibility的解決方案嗎? – 2013-03-25 21:37:25

+0

好吧,它基本上意味着你以編程的方式創建你想要執行它的程序,然後執行它...讀在[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

+0

此過程已經實現到宏和我希望從中受益。然後,如果它將被運行,它可以顯示一個阻止程序運行的msgBox。 – 2013-03-26 11:25:40