Connect to Rhino from Excel

I pulled the vba script below from another posts. I’m new to Rhinoscript and can not determine why Rhino opens but then terminates when the script ends. (@ the End Sub). I also had to add a visible true command in there to see rhino but it terminates at the end of the sub. Here is the code:

' Main subroutine
Sub Main()
  
  Dim RhinoApp As Object
  Dim Rhino As Object ' e.g. RhinoScript
  Dim strFile As Variant
  
  Set RhinoApp = ConnectToRunningRhino()
  If (RhinoApp Is Nothing) Then
    MsgBox "Failed to get Rhino interface object"
    Exit Sub
  End If
  
  Set Rhino = RhinoApp.GetScriptObject()
  If (Rhino Is Nothing) Then
    MsgBox "Failed to get RhinoScript object"
    Exit Sub
  End If

  strFile = Rhino.OpenFileName("Open", "RhinoApp 3D Models (*.3dm)|*.3dm|")
  If Not IsNull(strFile) Then
    Rhino.PrintEx strFile
  End If
  
End Sub

' Helper function
Function ConnectToRunningRhino()

  Dim RhinoApp As Object
  
  On Error Resume Next
  Set RhinoApp = CreateObject("Rhino5x64.Interface")
  If (Err.Number <> 0) Then
    'MsgBox "Unable to create Rhino5x64.Interface object"
    Exit Function
  End If
  
  Set ConnectToRunningRhino = RhinoApp

End Function

Hi @eric.bunn,

This statement creates an instance of Rhino:

Set RhinoApp = ConnectToRunningRhino()

However, since RhinoApp is a local variable to the Main subroutine, when the subroutine ends, the local variable goes out of scope and, thus, Rhino terminates.

– Dale

Thanks Dale.

I found that if I declared it at the macro level I could make if visible and run “_Open” command to get the strFile to open. It would stay open until I released the Rhino Object.

Eric

Is there a way to control the window size and position of Rhino. I’ve tried using the Windows API and cannot control the size and position.

Hi Eric,

SetWindowPos is probably what you want. Rhino.WindowHandle will return Rhino’s main window handle.

– Dale

Thanks. I’ll try that out.

Eric

This Helper Function was posted and works to open Rhino 5. I have two versions of Rhino on my laptop: 5 and 6. How do I get this to work for 6?

Eric

’ Helper function
Function ConnectToRunningRhino()

Dim Rhino As Object

On Error Resume Next
Set Rhino = CreateObject(“Rhino5x64.Interface”)
If (Err.Number <> 0) Then
'MsgBox “Unable to create Rhino5x64.Interface object”
Exit Function
End If

Set ConnectToRunningRhino = Rhino

End Function

This should help:

https://developer.rhino3d.com/api/rhinoscript/introduction/external_access.htm

– Dale

I should mention that I am trying to do this from Microsoft Excel through VBA. The code for Rhino 5 posted above works. I cannot get it to work for Rhino 6. I am setting up a reference to the Rhino.tlb library but it fails to open the software.

I’ve gotten the code below to open Rhino6 from Excel VBA but only if I declare Rhino as an Object. When I set a reference to the Rhinoceros Type Library (C:\Program Files\Rhino 6\System\Rhino.tlb) the code no longer works. This code works for Rhino 5 but not Rhino 6. I’ve tried Rhino.Application and Rhino.Application.6.

Thanks in advance for all the help.

Eric

’ Main subroutine
Private Sub main()

'Dim RhinoApp As Object
Dim RhinoApp As RhinoApplication
Dim Rhino As Object ’ e.g. RhinoScript
Dim strFile As Variant

Set RhinoApp = ConnectToRunningRhino()
If (RhinoApp Is Nothing) Then
MsgBox “Failed to get Rhino interface object”
Exit Sub
End If

Set Rhino = RhinoApp.GetScriptObject()
If (Rhino Is Nothing) Then
MsgBox “Failed to get RhinoScript object”
Exit Sub
End If

strFile = Rhino.OpenFileName(“Open”, “RhinoApp 3D Models (.3dm)|.3dm|”)
RhinoApp.Visible = True
If Not IsNull(strFile) Then
Rhino.PrintEx strFile
End If

End Sub

’ Helper function
Private Function ConnectToRunningRhino()

Dim RhinoApp As Object

On Error Resume Next
Set RhinoApp = CreateObject(“Rhino.Application”)
If (Err.Number <> 0) Then
'MsgBox “Unable to create Rhino5x64.Interface object”
Exit Function
End If

Set ConnectToRunningRhino = RhinoApp

End Function

Hi @eric.bunn,

Is this still an open issue?

– Dale

No, I cannot get the Rhino6 object unless I declare it as and object. I cannot use intellisence when declaring it as an object. I set a reference for the Rhino 6 tlb file but get an error when running the code.

Eric

Hi @eric.bunn,

I can repeat this. My only suggestion is to continue to late bind using CreateObject.

https://mcneel.myjetbrains.com/youtrack/issue/RH-56504

– Dale

Yes you are correct and I’ve already determined that late binding will work and it is currently what I am using. I do not get access to intellisense this way and although it isn’t a deal breaker it is not ideal. Rhino 5 I can early bind no problem, Rhino 6 I cannot.

I’ll continue using it this way until someone offers up a solution. Thanks for the help.

Eric