11

I have an Excel file with a large set of VBA code. There are 4 public subroutines that take no parameters that can be called by the user when the document is opened in Excel, these manipulate the data in the various sheets as needed. We have a large Java application that we would like to interact with this document by calling the Macros from the Java environment. The point is that we only have to write the VBA code once and then Java can call it for execution. Furthermore, we want to assume that the user of the Java application does not necessarily have immediate access to Excel, but is operating on a Windows machine. How should one go about doing this?

Do we compile the VBA code into a DLL, and call it from within Java? How do you compile the DLL, does that require the use of Visual Studio? How do we call the DLL from Java? Should we try some sort of COM object?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Reivax
  • 135
  • 1
  • 1
  • 10
  • Just to clarify, it's a local (client side) Java application? – home Jul 26 '11 at 16:49
  • Yes, the Excel document is local to the Java program, and the Java program is entirely client-side. – Reivax Jul 26 '11 at 17:00
  • Did you look at this http://stackoverflow.com/questions/2805763/call-a-vb-macro-from-java-code? – home Jul 26 '11 at 17:04
  • VBScript and VBA are not the same thing, so I'm hesitant to say that is the final solution. Also, it mandates that the user have Excel on their machine. This is something we're flexible on, but would rather not require it. Is possible to do it without installing the Office suite on the client's machine? We can have it to create the DLLs, but would rather not mandate it's use. – Reivax Jul 26 '11 at 17:15
  • Furthermore, the command line options of Excel do not support immediate execution of a Macro. http://office.microsoft.com/en-us/excel-help/command-line-switches-for-excel-HA010158030.aspx This is therefore not a viable solution. – Reivax Jul 26 '11 at 17:59
  • 2
    So your data is in Excel, and you want to call Excel macros in the document to process the data, but you don't want to require Excel be installed on the client. I can't see how that's going to work: even when you've figured out how to call VBA from Java, if there's no Excel then that seems like a bit of a barrier... – Tim Williams Jul 26 '11 at 18:11
  • Yea I too think it's not entirely possible. We may try Apache's POI to handle processing. – Reivax Jul 26 '11 at 20:52

1 Answers1

9

I basically see three options for calling VBA code in Excel from a Java application:

  1. Java COM Bridge: There are several tools available that allow you to call COM or COM Automation components from Java. Excel is such a component. I know of Jacob and JCom, but there might more such tools available.

  2. Java / VBScript / COM Automation: Since you obviously don't need to pass data to the VBA code, the simplest solution is probably to write a VBScript that starts Excel, opens the document, calls the macro and closes Excel. This script is started from Java with Runtime.getRuntime().exec("cmd /c start script.vbs");

  3. JNI: You could write a specific DLL for your applications. It implements the JNI interface so it can be called from Java. And its implementation uses COM calls to work with Excel. Such a DLL is best written with VisualStudio and it's support for C++ to call COM objects.

Whatever your solution will be, you basically want to execute the following commands on Excel automation interface (sample in VBScript):

Dim xl
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("workbook.xlsx")
xl.Application.Run "MyMacro"
xl.Application.Quit
Set xl = Nothing 

You cannot compile VBA code into a DLL. There exists no tool for that (in contrast to the full Visual Basic).

I hope this answer is helpful even though I didn't understand what you mean by: "we want to assume that the user of the Java application does not necessarily have immediate access to Excel, but is operating on a Windows machine."

Codo
  • 75,595
  • 17
  • 168
  • 206
  • Sounds like a great start, thank you. I think I'll do this: Create a COM object that replicates the existing VBA. Then, create a dll that calls the COM functions, and have the Java load the dll, and call it's functions. – Reivax Jul 26 '11 at 18:39
  • Clarification of your quote of mine: We assume that the client is on a Windows machine, but that none of the office suite (including Excel) is installed. Therefore, if it requires opening Excel to do so, it is not a viable solution. – Reivax Jul 26 '11 at 18:40
  • 1
    Now I'm confused... How did you plan to run VBA code without Excel installed? And why would you want to write a COM object (in what language anyway?) and then a wrapper (in C++ or whatever) and finally call it from Java if you have to rewrite the VBA code anyway? Why not make it many times simpler and rewrite it in Java? – Codo Jul 26 '11 at 21:22
  • Unfortunately this is not really a good answer because there is no good answer. Jacob and the other COM options provided are poorly made and will not enable a good integration at all between java and MS tools. –  Jan 15 '20 at 10:31