Custom ribbon button doesn't execute VBA sub

Custom ribbon button doesn't execute VBA sub

sv.lucht
Explorer Explorer
159 Views
2 Replies
Message 1 of 3

Custom ribbon button doesn't execute VBA sub

sv.lucht
Explorer
Explorer

I try to create custom ribbon tabs that get called when different criteria are met. The VBA code resides in StampMaker.ivb library in the UIManager module and everything works fine. I can create and destroy tabs,panel,buttons, but I cannot make the buttons work. The functions get never called. I tried to simplify the code, as seen below. I tried to get help from Gemini. But nothing works. So I would be very grateful for any suggestions. Here is a simplified version of the code just for troubleshooting. I checked all the names. I counted the ControlDefinitions and the count increases when the object is created, so the object exists. I can delete and recreate it just fine. But I cannot make the link to the Sub working. I tried to call a function in the default library, but this doesn't work either. I tried MacroButtons, but the same problem.  The AI suggest that I should alter the security settings, but I cannot find it in Inv 2025 Pro.

Thank you very much in advance.

Public Sub RunCleanButtonTest()
' --- Phase 1: Aggressive Cleanup ---
'Const TEST_MACRO_NAME As String = "Default.Module1.testio"
Const TEST_MACRO_NAME As String = "StampMaker.UIManager.testprompt"
Const TEST_TAB_NAME As String = "MyTestTab"
Const TEST_PANEL_NAME As String = "MyTestPanel"

' Delete the Control Definition
On Error Resume Next
ThisApplication.CommandManager.ControlDefinitions.item(TEST_MACRO_NAME).delete
Debug.Print "Old Control Definition deleted (if it existed)."
On Error GoTo 0

' Delete the Ribbon Tab
Dim zeroRibbon As Inventor.Ribbon
Set zeroRibbon = ThisApplication.UserInterfaceManager.Ribbons.item("ZeroDoc")
On Error Resume Next
zeroRibbon.RibbonTabs.item(TEST_TAB_NAME).delete
Debug.Print "Old Ribbon Tab deleted (if it existed)."
On Error GoTo 0

' --- Phase 2: Create a single, simple button ---
Debug.Print "Creating new UI..."

' Create Tab and Panel
Dim newTab As RibbonTab
Set newTab = zeroRibbon.RibbonTabs.Add("My Test", TEST_TAB_NAME, "ClientID_TestTab")
Dim newPanel As RibbonPanel
Set newPanel = newTab.RibbonPanels.Add("My Panel", TEST_PANEL_NAME, "ClientID_TestPanel")

' Create Button Definition
Dim buttonDef As ButtonDefinition
Set buttonDef = ThisApplication.CommandManager.ControlDefinitions.AddButtonDefinition( _
"Click Me", _
TEST_MACRO_NAME, _
CommandTypesEnum.kNonShapeEditCmdType)

' Add button to panel
Call newPanel.CommandControls.AddButton(buttonDef, True)

Debug.Print "--- Test UI created. Please click the 'Click Me' button on the 'My Test' tab. ---"
End Sub


Public Sub testprompt()
Debug.Print ("HEUREKA! it works")
Debug.Print ("HEUREKA! it works")
Debug.Print ("HEUREKA! it works")
End Sub

 

 

0 Likes
160 Views
2 Replies
Replies (2)
Message 2 of 3

sv.lucht
Explorer
Explorer

This code works and executed the ControlDefinition. It seems that you need a MacroControlDefinition and a called function that resides inside the default.ivb, which sucks because I wanted to keep my code portable in an external ivb. 

Public Sub CreateMinimalMacroButton()
' --- 1. Define the names for the macro and UI elements ---
Const MACRO_FULL_NAME As String = "Module1.MyTargetSubroutine"
Const TAB_ID As String = "StampMaker_MinimalTab_1"
Const PANEL_ID As String = "StampMaker_MinimalPanel_1"

Dim uiMgr As UserInterfaceManager
Set uiMgr = ThisApplication.UserInterfaceManager

' --- 2. Clean up any old versions of these UI elements ---
' Using "On Error Resume Next" is a simple way to delete items
' without causing an error if they don't exist yet.
On Error Resume Next

' Delete the Ribbon Tab (this also removes the panel and the button on it)
uiMgr.Ribbons.item("ZeroDoc").RibbonTabs.item(TAB_ID).delete

' Delete the Control Definition (the "brain" of the button)
ThisApplication.CommandManager.ControlDefinitions.item(MACRO_FULL_NAME).delete

' Return to normal error handling
On Error GoTo 0

' --- 3. Create the new UI ---

' Get the ribbon that's visible when no document is open
Dim zeroRibbon As Ribbon
Set zeroRibbon = uiMgr.Ribbons.item("ZeroDoc")

' Create the new tab
Dim myTab As RibbonTab
Set myTab = zeroRibbon.RibbonTabs.Add("Minimal Test", TAB_ID, "ClientID_MinimalTab")

' Create a panel on the tab
Dim myPanel As RibbonPanel
Set myPanel = myTab.RibbonPanels.Add("Test Panel", PANEL_ID, "ClientID_MinimalPanel")

' Create the Macro Definition - this links the button to your code
Dim macroDef As MacroControlDefinition
Set macroDef = ThisApplication.CommandManager.ControlDefinitions.AddMacroControlDefinition(MACRO_FULL_NAME)

' Add the actual button to the panel
Call myPanel.CommandControls.AddMacro(macroDef)

' --- 4. Notify the user ---
MsgBox "Minimal macro button has been created." & vbCrLf & _
"Look for the 'Minimal Test' tab in your ribbon.", vbInformation

End Sub
0 Likes
Message 3 of 3

YuhanZhang
Autodesk
Autodesk

Hi @sv.lucht ,

 

This is a limitation that the macro should be in the Application project for the ribbon button to call, see documentation below:

 

         Inventor 2025 Help | ControlDefinitions.AddMacroControlDefinition Method | Autodesk

 

But you may find a workaround from below post:

 

        Solved: Ribbon Macro button to call macro in second ivb project - Autodesk Community

 

You can call a macro in Application project using your ribbon button, and in your macro it can run another macro in a custom .ivb file.

 

Hope this 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