add button for VBA macro to custom ribbon panel

add button for VBA macro to custom ribbon panel

Curtis_Waguespack
Consultant Consultant
3,970 Views
4 Replies
Message 1 of 5

add button for VBA macro to custom ribbon panel

Curtis_Waguespack
Consultant
Consultant

 

I would like to add buttons to a custom ribbon panel, and I'd like those buttons to point to macros. I can't find any examples to do this.

 

Here is what I have so far, based on the sample code from the API help. This creates a new ribbon panel and adds buttons for standard commands. I'd like to add a 4th button that points to a macro.

 

Thanks in advance,

Curtis

 

 

 

 

Sub AddPanelToToolsTab()
    ' Get the ribbon associated with the part document
    Dim oPartRibbon  As Inventor.Ribbon
    Set oPartRibbon = ThisApplication.UserInterfaceManager.Ribbons.Item("Part")

    'Get the "Tools" tab
    Dim oTab As RibbonTab
    Set oTab = oPartRibbon.RibbonTabs.Item("id_TabTools")
    
    oTabName = "Custom"
    
    Dim oPanel As RibbonPanel
    For Each oPanel In oTab.RibbonPanels
        If oPanel.DisplayName = oTabName Then
            oPanel.Delete 'remove it
        End If
    Next

    ' Create a panel
    Set oPanel = oTab.RibbonPanels.Add(oTabName, "ToolsTabCustomPanel", "SampleClientId")

    ' Get the update commands
    Dim oDef1 As ButtonDefinition
    Set oDef1 = ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd")

    Dim oDef2 As ButtonDefinition
    Set oDef2 = ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd")

    Dim oDefs As ObjectCollection
    Set oDefs = ThisApplication.TransientObjects.CreateObjectCollection

    oDefs.Add oDef1
    oDefs.Add oDef2

    ' Create a split button control
    Call oPanel.CommandControls.AddSplitButton(oDef1, oDefs, False)

    ' Get the rebuild command
    Dim oDef3 As ButtonDefinition
    Set oDef3 = ThisApplication.CommandManager.ControlDefinitions.Item("AppRebuildAllWrapperCmd")

    ' Create a button control
    Call oPanel.CommandControls.AddButton(oDef3, False)
    
'    'create button for custom macro
'    Dim oDef4 As ButtonDefinition
'    Call oPanel.CommandControls.AddButton(oDef4, False)
    

End Sub

EESignature

0 Likes
Accepted solutions (1)
3,971 Views
4 Replies
Replies (4)
Message 2 of 5

ruthsteed
Autodesk
Autodesk
Accepted solution

Hi Curtis,

 

You're almost there.  You need to create a MacroControlDefinition object and then use CommandControls.AddMacro

 

Try adding these 3 lines to your VBA code:

    Dim oMacroDef As MacroControlDefinition
    Set oMacroDef = ThisApplication.CommandManager.ControlDefinitions.AddMacroControlDefinition("Module1.MyMacroCommand")
    oPanel.CommandControls.AddMacro oMacroDef, False

 

"Module1.MyMacroCommand" should be replaced with whatever your macro is named.  In this example, "Module1" is the name of the module within your project, and "MyMacroCommand" is the name of the sub you want to execute when the button is pressed.

 

Hope this helps,

-Ruth



Ruth Steed
Inventor Development Team
Autodesk, Inc.

Message 3 of 5

Curtis_Waguespack
Consultant
Consultant

Hi ruthsteed,

 

Thanks for the quick reply, that's exactly what I was looking for!

EESignature

0 Likes
Message 4 of 5

Anonymous
Not applicable

Hello Ruth,

i had the same problem and this has worked pretty fine for me! Thanks a lot!
But Inventor keeps deleting my configuration of the Macro-Buttons as soon as i close and reopen the application.

Is there a way to save these settings?

Something like "ThisApplication.UIconfig.save"

0 Likes
Message 5 of 5

ruthsteed
Autodesk
Autodesk

Hi Tim,

Unfortunately, there isn't any built-in functionality to do what you want. If you have a lot of these, you can add another button to your zero document ribbon (or part if you usually start Inventor by opening a part document) to execute a macro that adds these macro button definitions. You may want to add this as an IdeaStation suggestion.

Regards,

-Ruth



Ruth Steed
Inventor Development Team
Autodesk, Inc.

0 Likes