| FAQ: VBA Support |
|
Using Mathematica Link for Excel 3, you can call Mathematica from the Excel VBA environment. The syntax of the Mathematica VBA function has changed since version 2. In version 3, the API is much cleaner. It is now much easier to do things such as return arrays. We are updating the VBA-side documentation which should eventually make it into the manual. Until then, here are a few examples to give you the idea of how to do it.
result = Application.Run("Mathematica", Array("Plus", 2, 2))
As you can see, Mathematica expressions are represented using 0-based, 1-D arrays. You can nest these as much as you want. The first (0) element of the array is the head of the function and the rest are arguments to the function. Symbols have no arguments. 0 arguments can be represented as shown above. When you evaluate an array-based expression, by default, results are returned in that form as well. If you want to evaluate a text expression, you can do that too. In this case, by default, results are returned as text.
result = Application.Run("Mathematica", "Plus[2, 2]")
An optional second argument allows you to specify a return format.
result = Application.Run("Mathematica", "Range[3]", "Expression")
Returning results as native VBA arrays is also possible. An optional third argument can be used to specify options relating to how the array is formatted.
result = Application.Run("Mathematica", Array("IdentityMatrix", 2))
Normally, connecting to Mathematica is managed automatically, but if you want to connect and disconnect from Mathematica explicitly, you can.
Call Application.Run("Mathematica", True)
|



