JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java。這是jacob官方網站的原話,本人就不再多說了。jacob的實現有些像封裝了com功能的jni調用的集合及承載com對象的容器。jacob作者Dan Adler使用了c++編寫了一批程序庫實現對com的引用/承載/調用,然后使用java的jni技術調用這些程序庫,實現JAVA-COM Bridge。
Sub MyWordMacro(strPassedParam As String) MsgBox strPassedParam End Sub
而訪問這個MyWordMacro宏的vb代碼如下:
Sub AutomateWord_OpenDoc() Dim wrdApp As Object Dim wrdDoc As Object Dim strFileName As String Set wrdApp = CreateObject("Word.Application") On Error GoTo DocError ' Replace the following example string value with the path and ' file name of the template containing your macro. strFileName = "<Path and Filename of template>" ' Open the document and set a variable equal to a new blank ' document and its underlying template. Set wrdDoc = wrdApp.Documents.Add(strFileName) ' Run the macro. (Replace "MyWordMacro" with the name of your macro.) wrdDoc.MyWordMacro ("This is a test.") DocError: If Err.Number <> 0 Then Msgbox Err.Descr 這樣就能在vb中訪問word的macro。。
Sub AutomateWord_OpenDoc() Dim wrdApp As Object Dim wrdDoc As Object Dim strFileName As String Set wrdApp = CreateObject("Word.Application") On Error GoTo DocError '包含marco的word文件 strFileName = "c:/MacroTest.doc" '打開文件 Set wrdDoc = wrdApp.Documents.Open(strFileName) '運行宏 wrdDoc.MyWordMacro ("This is a test.")DocError: If Err.Number <> 0 Then MsgBox Err.Description '退出word wrdApp.Quit '清除內存 Set wrdApp = Nothing Set wrdDoc = NothingEnd SubPRivate Sub Command1_Click() AutomateWord_OpenDocEnd Sub