Ribbon Macro button to call macro in second ivb project

Ribbon Macro button to call macro in second ivb project

cshunt
Enthusiast Enthusiast
880 Views
3 Replies
Message 1 of 4

Ribbon Macro button to call macro in second ivb project

cshunt
Enthusiast
Enthusiast

I am trying to a a button to the ribbon that will call a macro that is not located in the default.ivb file. Is there anyway to link a button to a procedure located in a second project?

 

The below code add the tab and button to the ribbon while in the drawing environment. If there is a procedure call MyMessage located in the Default.ivb project, everything works. If I move the procedure to the second project I cannot seem to get it to be called.

using something like "VbaProject2.addbuttontoribbon.MyMessage" doesn't seem to work.

 

Any ideas would be welcomed. Thanks!!

 

 

The tree starts out like this:

ApplicationProject (Default.ivb)

VbaProject 2 (VbaProject2.ivb)

-addbuttontoribbon

  Sub MyMessage()

 

Sub AddButton2()
        Dim oDoc As DrawingDocument
        Set oDoc = ThisApplication.ActiveDocument

        Dim oRibbon As Ribbon: Set oRibbon = ThisApplication.UserInterfaceManager.Ribbons.item("Drawing")

        Dim oRibbonTabs As RibbonTabs: Set oRibbonTabs = oRibbon.RibbonTabs
        Dim oRibbonTab As RibbonTab
        For Each oRibbonTab In oRibbonTabs
            If oRibbonTab.InternalName = "Id_TabTab" Then
            Exit For
            End If
        Next
        If oRibbonTab Is Nothing Then Set oRibbonTab = oRibbon.RibbonTabs.Add("Tab", "Id_TabTab", "", "", True, False) 'only works on initial run
           
        Dim oRibbonPanels As RibbonPanels: Set oRibbonPanels = oRibbonTab.RibbonPanels
        Dim oRibbonPanel As RibbonPanel
        For Each oRibbonPanel In oRibbonPanels
            If oRibbonPanel.InternalName = "Drawing.id_TabTab.Tab" Then
            Exit For
            End If
        Next
        If oRibbonPanel Is Nothing Then Set oRibbonPanel = oRibbonTab.RibbonPanels.Add("Tab", "Drawing.id_TabTab.Tab", "") 'only works on initial run
        
        
    Dim oCommandControl As CommandControl
    Dim oCommandControls As CommandControls: Set oCommandControls = oRibbonPanel.CommandControls
    
'add a macrobutton with non macro icon

    Set oCommandControl = Nothing
     For Each oCommandControl In oCommandControls
        If oCommandControl.InternalName = "macro:AddButtonToRibbon.MyMessage" Then
        oCommandControl.Delete
        Set oCommandControl = Nothing
        Exit For
        End If
     Next
     
     
    Dim oMacroControlDef As MacroControlDefinition
    Set oMacroControlDef = ThisApplication.CommandManager.ControlDefinitions.AddMacroControlDefinition("AddButtonToRibbon.MyMessage")
        
       If oCommandControl Is Nothing Then Set oCommandControl = oRibbonPanel.CommandControls.AddMacro(oMacroControlDef, False, True, "", False)
       
        oRibbonTab.Visible = True
        oRibbonPanel.Visible = True

End Sub
0 Likes
Accepted solutions (1)
881 Views
3 Replies
Replies (3)
Message 2 of 4

YuhanZhang
Autodesk
Autodesk
Accepted solution

Please notice that this is not supported to call the macro in a user/document VBA project using macro control definition, see the Description for MacroOrProgram argument in the AddMacroControlDefinition method:

 

http://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-B769BA2B-85D0-4026-A677-FDC660306389

 

But you can try to call another macro in different project in your macro, below is a sample:

    1. Download the MyProj.ivb to C:\Temp folder, it has a simple macro.

    2. Copy below VBA code to your ApplicationProject.

Sub CallMacroFromAnotherProj()
    ' you should specify the IVB path and name firstly.
    Dim sIVBPath As String
    sIVBPath = "C:\temp\MyProj.ivb"
    
    Dim sIVBName As String
    sIVBName = "TestNew"
    
    Call ThisApplication.VBAProjects.Open(sIVBPath)
    
    Dim oVBAProj As InventorVBAProject
    Dim oTempProj As InventorVBAProject
    
    For Each oTempProj In ThisApplication.VBAProjects
        If oTempProj.ProjectType = kUserVBAProject Then
            If oTempProj.Name = sIVBName Then
                Set oVBAProj = oTempProj
                Exit For
            End If
        End If
    Next
    
    Debug.Print oVBAProj.Name
    
    Dim oVBAComp As InventorVBAComponent
    Set oVBAComp = oVBAProj.InventorVBAComponents.Item("Module1")
    
    Debug.Print oVBAComp.Name
    
    Dim oVBAMember As InventorVBAMember
    Set oVBAMember = oVBAComp.InventorVBAMembers.Item("test")
    
    Debug.Print oVBAMember.Name
    oVBAMember.Execute
    
End Sub

    3. Add the macro to ribbon, and try it.

 

Hope above helps.

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 4

YuhanZhang
Autodesk
Autodesk

Seems the attachment is missing, reattach it.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 4 of 4

cshunt
Enthusiast
Enthusiast

Thanks for the reply. I have a function very similar the one you posted, it pulls up a user form for controls.

I now realize that a button for a macro cannot be placed on the ribbon because on startup, that macro is not available until the external ivb project is loaded.

 

It would be nice if inventor would allow multiple .ivb projects to be loaded on the options screen. i.e. separatoed by a ";" or comma.

 

Dim oApp As Application: Set oApp = ThisApplication
Dim oProjectName As String: oProjectName = "InventorMacros"
Dim oModuleName As String: oModuleName = "Module1"
Dim oMacroName As String: oMacroName = "ShowExternalForm"

Dim ProjectLocation As String: ProjectLocation = "c:\temp\InventorMacros.ivb"

Dim oProjects As InventorVBAProjects: Set oProjects = ThisApplication.VBAProjects

Dim oProject As InventorVBAProject

Dim ProjectFound As Boolean: ProjectFound = False
Set oProjects = ThisApplication.VBAProjects
              For Each oProject In oProjects
                    If oProject.Name = oProjectName Then
                        ProjectFound = True
                        Exit For
                    End If
                Next
If ProjectFound = False Then
    Call ThisApplication.VBAProjects.Open(ProjectLocation)
    Set oProject = ThisApplication.VBAProjects.Item(ThisApplication.VBAProjects.count)
End If

Call oProject.InventorVBAComponents(oModuleName).InventorVBAMembers(oMacroName).Execute 'module1, macro 2
0 Likes