Excel VBA Accessing Java

Java/J2EE COM Interoperability Products Page

This example follows on from the J-Integra® Visual BASIC to Java late binding example, and assumes you still have the JVM running, and that you got that example working.

You will put the default value associated with the Simple object (which J-Integra® automatically maps to its toString() method) into cells when the user clicks on them.

These are the steps:

  1. Start up Excel, and then click on Tools|Macro|Visual Basic Editor:

  2. Double-click on Sheet 1 in the list of objects:

  3. Add the following code which gets called when you click on the spreadsheet:

    Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Dim simple As Object
    Set simple = GetObject("firstjvm:Simple")
    Taget = simple ' Uses 'toString()" on Simple
    ' If the Java Simple class had a property xyz:
    ' Target = simple.xyz
    End Sub

  4. Go back to the spreadsheet by clicking on View|Object

  5. Click anywhere on the spreadsheet, the cell you click on should be filled with the default value associated with the Java Simple object (the result of calling toString() on it):

By running the 'java2com' tool on your existing Java classes, you should be able to create instances of them, and invoke their methods and properties from VBA. J-Integra® renders accessible all public methods and fields (even if static).