VBA - Add a custom macro on a custom panel

VBA - Add a custom macro on a custom panel

TONELLAL
Collaborator Collaborator
500 Views
3 Replies
Message 1 of 4

VBA - Add a custom macro on a custom panel

TONELLAL
Collaborator
Collaborator

Hello,

I'm trying to add a macro on a custom panel.

I create the tab, the panel, the macro button... but I can't make the button launch the macro !

Here's the code, what's wrong ?

 

Public Sub test()
    MsgBox ("Hello")
End Sub

Private Sub create_macro_button()

Dim oAssemblyRibbon As Ribbon
Dim oCustomTab As RibbonTab
Dim oCustomPanel As RibbonPanel

Set oAssemblyRibbon = ThisApplication.UserInterfaceManager.Ribbons.item("Assembly")

'Create tab "Custom"
On Error Resume Next
Call oAssemblyRibbon.RibbonTabs("TabCustomIntName").Delete
On Error GoTo 0
Call oAssemblyRibbon.RibbonTabs.Add("Custom", "TabCustomIntName", "CustomId")
Set oCustomTab = oAssemblyRibbon.RibbonTabs("TabCustomIntName")

'Create panel "Custom"
On Error Resume Next
Call oCustomTab.RibbonPanels.item("PanelCustomIntName").Delete
On Error GoTo 0
Call oCustomTab.RibbonPanels.Add("Custom", "PanelCustomIntName", "CustomId")
Set oCustomPanel = oCustomTab.RibbonPanels.item("PanelCustomIntName")

'****************************************************************
'Macro definition

macroInternalName = "Macro"
On Error Resume Next
Call ThisApplication.CommandManager.ControlDefinitions.item(macroInternalName).Delete
On Error GoTo 0

Dim oMacroDef As MacroControlDefinition

Set oMacroDef = ThisApplication.CommandManager.ControlDefinitions.AddMacroControlDefinition("Module1.test")
Call oCustomPanel.CommandControls.AddMacro(oMacroDef, False)

End Sub
0 Likes
501 Views
3 Replies
Replies (3)
Message 2 of 4

DRoam
Mentor
Mentor

The internal name for a macro Control Definition takes on this format:

"macro:<ModuleName>.<SubName>"

 

So for your macro it would be this:

macroInternalName = "macro:Module1.test"

 

Everything else in your code looks good. I ran it with that change and it's working fine for me. Try making that change and see if it fixes it.

0 Likes
Message 3 of 4

TONELLAL
Collaborator
Collaborator

Thanks for your answer, but on my computer... nothing happen 😞

I copy my code in a new module called "Module1", declare MacroInternalName = "Macro:Module1.test"

The button is created, but nothing happen when I click it...

I'm on Inventor 2018.

 

0 Likes
Message 4 of 4

DRoam
Mentor
Mentor

Strange, I just don't see anything wrong. Can you send me a screenshot of your VBA editor with both subs ("test" and "create_macro_button") visible?

0 Likes