Starting and Connecting to a COMSOL Multiphysics Server
The most common approach to start and connect Excel to a COMSOL Multiphysics server is by opening a model using OpenModel() from the RibbonUtil utilities. However, LiveLink™ for Excel® also provides methods for starting and connecting to a server manually.
Ordinarily you would run StartComsolServer() to start the COMSOL Multiphysics server:
Boolean ComsolUtil.StartComsolServer(useGraphics As Boolean)
This command will start the COMSOL Multiphysics server in graphics mode when useGraphics is set to True on the local computer. A port number, which is used for the communication between the COMSOL server and Excel, is automatically assigned by the COMSOL server. The default number is 2036, but if other COMSOL servers are running then a higher number has to be chosen. It also returns a Boolean to indicate if the COMSOL server has started (True) or failed to start (False).
To connect Excel to the COMSOL server using the default port number, simply use the command:
Void RibbonUtil.Connect([hostname As String], [portnumber As Int], [login As String],[pwd As String])
where hostname, portnumber, login, and pwd are optional argument that set the connection credential: computer name, the port number the server is listening to, the user name and the password respectively. If you are already connected to a server and try to connect it again, nothing happens and you will remain connected. If however you try connect to a different server, it will disconnect automatically from the first one and connect to the specified one.
You can check if the RibbonUtil service is already connected to a server with the IsConnected() method:
Boolean RibbonUtil.IsConnected()
This returns True if already connected, False else.
To disconnect from the server use Disconnect():
Void RibbonUtil.Disconnect()
Code for use with VBA
Below you find an example showing how to connect manually to a COMSOL Multiphysics server; feel free to copy and paste it to the VBA editor.
Sub ConnectServerExample()
 
Dim ComsolUtil As ComsolUtil
Dim RibbonUtil As IRibbonUtil
 
Set ComsolUtil = CreateObject("comsolcom.comsolutil")
Set RibbonUtil = ComsolUtil.GetRibbonUtil
 
If Not RibbonUtil.IsConnected Then
' Start a Multiphysics server
ok = ComsolUtil.StartComsolServer(True)
' Return an error if not started properly
If ok = False Then
MsgBox "no server " & ComsolUtil.get_errormessage
Exit Sub
End If
' Get the port number used
port = ComsolUtil.get_port
' Connect to the server using the specified port number (RibbonUtil.Disconnect
RibbonUtil.Connect "localhost", port
End If
End Sub