Create A Macro Button Using A Inventor VBA Macro

Create A Macro Button Using A Inventor VBA Macro

isocam
Collaborator Collaborator
351 Views
2 Replies
Message 1 of 3

Create A Macro Button Using A Inventor VBA Macro

isocam
Collaborator
Collaborator

Can anybody help?

 

Is it possible to create a macro button using an Inventor VBA macro?

 

All I need, for testing purposes, is a button that, when pressed, shows "HELLO WORLD".

 

Many thanks in advance!

 

Darren

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

WCrihfield
Mentor
Mentor

Hi @isocam.  That is a complex task, partially because of how custom it is.  Generally speaking, you do not have one VBA macro that does some sort of task, and also adds itself as a button into your ribbon.  So, you would need a regular VBA Module stored within the application VBA project, and its first/primary method must be a Sub routine (not a Function), and it must not have any 'input' parameters.  Then there must be a different VBA macro specifically for adding that other macro to the ribbon as a macro button.  This second macro (for creating the button) needs to specify which Environment / Ribbon to work with (usually the active one), which RibbonTab within that Ribbon, which RibbonPanel within that RibbonTab, where to place it among any existing controls within that panel, and so on.  The ControlDefinition must either already exist, or be created.  Then the CommandControl (actual button) needs to be created that uses that ControlDefinition.  Below are two VBA codes.  The first one is just for showing "HELLO WORLD!", and the second one is for creating the button.

Show the message: [Module named "HelloWorld", and Sub routine named "HelloWorld".]

Sub HelloWorld()
    Call MsgBox("HELLO WORLD!", vbOKOnly, "HELLO WORLD")
End Sub

Create the button for it:  [Module named "AddMacroButton", Sub routine named "AddMacroButton".]

Sub AddMacroButton()
    Dim UIMgr As Inventor.UserInterfaceManager
    Set UIMgr = ThisApplication.UserInterfaceManager
    
    Dim oRibbon As Inventor.Ribbon
    Set oRibbon = UIMgr.ActiveEnvironment.Ribbon
    
    Dim sTabName As String
    sTabName = UIMgr.ActiveEnvironment.DefaultRibbonTab
    
    Dim oRTab As Inventor.RibbonTab
    Set oRTab = oRibbon.RibbonTabs.Item(sTabName)
    oRTab.Visible = True
    oRTab.Active = True
    
    Dim oPanels As Inventor.RibbonPanels
    Set oPanels = oRTab.RibbonPanels
    
    Dim oFirstPanel As Inventor.RibbonPanel
    Set oFirstPanel = oPanels.Item(1)
    oFirstPanel.Visible = True
    
    Dim oCCs As Inventor.CommandControls
    Set oCCs = oFirstPanel.CommandControls
    
    Dim oCDs As Inventor.ControlDefinitions
    Set oCDs = ThisApplication.CommandManager.ControlDefinitions
    
    Dim oMacroCD As Inventor.MacroControlDefinition
    Dim oMCD As ControlDefinition
    For Each oMCD In oCDs
        If oMCD.InternalName = "HelloWorld.HelloWorld" Then
            Set oMacroCD = oMCD
            oMacroCD.Enabled = True
        End If
    Next
    If oMacroCD Is Nothing Then
        Set oMacroCD = oCDs.AddMacroControlDefinition("HelloWorld.HelloWorld")
        oMacroCD.Enabled = True
        'Call MsgBox("Macro ControlDefinition Not Found!", vbCritical, "Not Found")
        'Exit Sub
    End If
    
    Dim oMacroButtonControl As Inventor.CommandControl
    Set oMacroButtonControl = oCCs.AddMacro(oMacroCD, False, True)
    oMacroButtonControl.Visible = True
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Frederick_Law
Mentor
Mentor

https://forums.autodesk.com/t5/inventor-programming-ilogic/creating-an-icon-for-a-vba-macro-on-inven...

You can add "button" to Ribbon with custom icon.

 

You can create a form in VBA and add button to lunch VBA macro.

VBA-Form-01.jpg

0 Likes