Linking to Mathematica Kernel from Excel
- To: mathgroup at smc.vnet.net
- Subject: [mg59484] Linking to Mathematica Kernel from Excel
- From: "plizak" <plizak at gmail.com>
- Date: Wed, 10 Aug 2005 02:58:21 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I'm trying to write code to link to Mathematica automatically from
within MS Excel. Here is the code I have so far (below - It's VBA
code).
It does not work when I first open Excel, but if I have already
connected to the kernel prior and want to reconnect then it works. Do
I have to point to the location of the kernel? If so how?
Also, Am I clearing variables properly? What command should I use in
Mathematica to clear all variables???
Thanks in advance,
Peter
------ VBA code below --------------------------------------------
' If there is an error, go to "deal"
On Error GoTo deal
result = Application.Run("Mathematica", "$Version")
Debug.Print result
' If there is no link, then connect to the Kernel
If result = "$NoLink" Then
' Connect to the mathematica Kernel
Debug.Print "Connecting to Mathematica"
Application.Run "LinkOpenOrClose"
' If there is a link already, clear all variables
Else
' Clear the contents of the mathematica kernel
Debug.Print "Clearing Memory"
FunInput = "Clear[ ""Global `*""]"
result = Application.Run("Mathematica", FunInput)
Debug.Print result
End If
' check the version just to make sure we have a link
result = Application.Run("Mathematica", "$Version")
If result = "$NoLink" Then
MsgBox ("Error connecting to Mathematica Kernel. Please verify
Mathematica is installed and available and please try again.")
End If
Exit Sub
' do the following if we encounter an error
deal:
Debug.Print "Error Linking Mathematica: Error # ", Err
If Err = 1004 Then
result = Application.Run("Mathematica", "LinkOpenOrClose")
End If