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.Description ' Quit this instance of Word. wrdApp.Quit ' Clear variable memory. Set wrdApp = Nothing Set wrdDoc = Nothing End Sub
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