Run VBA from Inventor addin?!

Run VBA from Inventor addin?!

Anonymous
Not applicable
770 Views
2 Replies
Message 1 of 3

Run VBA from Inventor addin?!

Anonymous
Not applicable

Hello,
I wrote a VBA Code and now wanted it to start after click on save, but Autodesk deaktivated the option for makros.

I wonder now if this code still works, because I cant see any resault and the vb.net is new for me... maybe code doesnt work anymore or I make some misstakes?

http://adndevblog.typepad.com/manufacturing/vladimir-ananyev/page/6/

 

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

 

I edited

Module1 = CreateView
MyFunktion = VBAToExecute


The Module "CreateView" is in "ApplicationProject" (Default.ivb).

Alternative I can translate the VBA to a VB.net, but it was hard enough to me to create the vba code. 😞

0 Likes
Accepted solutions (1)
771 Views
2 Replies
Replies (2)
Message 2 of 3

Owner2229
Advisor
Advisor
Accepted solution

Hey, I used to use this too, so I know it works, but hey, here's a simplified version:

 

 

    For Each oProject As Inventor.InventorVBAProject In m_InventorApp.VBAProjects
        If oProject.Name <> "ApplicationProject" Then Continue For
        Dim oComp As Inventor.InventorVBAComponent = oProject.InventorVBAComponents("Module1")
        Dim oMember As Inventor.InventorVBAMember = oComp.InventorVBAMembers("MyFunction")
        Try
            oMember.Execute()
        Catch
        End Try
    Next

 

Just make sure all the called Subs are Public:

Public Sub MyFunction()
    'Code
End Sub

Also, I would suggest you learning VB.Net, translating all your rules to it (over time, e.g. half a year) and moving them to an AddIn. It'll give you much more options and control over what you're doing.

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 3

Anonymous
Not applicable

Thank you. Didnt name the Sub "Public". Now it works.
I think I will learn vb.net in the future. Tanks again and have a nice day!

0 Likes