Run VBA macro using Inventor API

By Vladimir Ananyev

Issue

Is there an example available showing how to run a VBA macro using the Inventor API?

Solution

The following VB samples look for ‘MyFunction’ in the Application’s ‘Module1’ and execute it.

VB.NET version

Public Sub RunMacro_VBA()

  Dim m_inventorApp As Inventor.Application = Nothing

  ‘ Try to get an active instance of Inventor

  Try

    m_inventorApp = System.Runtime.InteropServices _

     .Marshal.GetActiveObject("Inventor.Application")

  Catch ex As Exception

  End Try

  ‘ If not active, create a new Inventor session

  If m_inventorApp Is Nothing Then

    Dim inventorAppType As Type = System _

     .Type.GetTypeFromProgID("Inventor.Application")

    m_inventorApp = System.Activator _

         .CreateInstance(inventorAppType)

  End If

 

  Dim oP As Inventor.InventorVBAProject

  For Each oP In m_inventorApp.VBAProjects

    If oP.Name = "ApplicationProject" Then

      Dim oC As Inventor.InventorVBAComponent

      For Each oC In oP.InventorVBAComponents

        If oC.Name = "Module1" Then

          Dim oM As Inventor.InventorVBAMember

          For Each oM In oC.InventorVBAMembers

            If oM.Name = "MyFunction" Then

              oM.Execute()

            End If

          Next oM

        End If

      Next oC

    End If

  Next

End Sub

VBA version

Private Sub Command1_Click()

  Dim oA As Inventor.Application

  Set oA = GetObject(, "Inventor.Application")

 

  Dim oP As Inventor.InventorVBAProject

 

  For Each oP In oA.VBAProjects

    If oP.Name = "ApplicationProject" Then

      Dim oC As Inventor.InventorVBAComponent

      For Each oC In oP.InventorVBAComponents

        If oC.Name = "Module1" Then

          Dim oM As Inventor.InventorVBAMember

          For Each oM In oC.InventorVBAMembers

            If oM.Name = "MyFunction" Then

              Dim oO As Variant

              Call oM.Execute(oO)

            End If

          Next oM

        End If

      Next oC

    End If

  Next

 

End Sub

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal”>


Comments

One response to “Run VBA macro using Inventor API”

  1. Dear Sir,
    myfunction is filename? module1 is modulname?. what about the sub name of the macro??
    is it possible that you can describe each line…
    Best regards
    Aytekin

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading