Create a new Tools menu item using VBA

Create a new Tools menu item using VBA

isocam
Collaborator Collaborator
345 Views
2 Replies
Message 1 of 3

Create a new Tools menu item using VBA

isocam
Collaborator
Collaborator

Can anybody help?

 

Does anybody have any VBA or Vb.Net code that adds a new item to the tools menu called (for example) Button1 so that, when pressed, a message box appears stating "HELLO WORLD"?

 

This is for testing purposes.

 

Many thanks in advance!

 

Darren

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

WCrihfield
Mentor
Mentor

Hi @isocam.  By "tools menu", do you mean the Tools tab in the main Ribbon?  If so, is it OK to just put it into the "User Commands" RibbonPanel, where it would normally go if added it manually, or do you want to put it into a different existing RibbonPanel?  The Tools tab can be customized differently in different environments (while editing a part, or assembly, or drawing, or other situations), so which Environment do you want to put it into?  Plus, I think two codes would be needed, because the VBA macro shouldn't add itself to a panel, or it would be trying to do that every time you click it, plus it has to already exist before you can add it to a panel.  One code would be the VBA macro that just shows the message, then the other code would put it into the panel for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Michael.Navara
Advisor
Advisor

Here is the most simple iLogic example, how to create button in default AddIns tab

You can modify method 

Private Sub Button1_OnExecute(context As NameValueMap)

at line 52 to change the button behavior.

 

 

 

 

Sub Main()

TryDeleteButton()
CreateButton()

End Sub

'Can be replaced with any GUID
Const ClientId = "{6FE44211-19D7-4CFF-855E-DCF34864AD19}"
Const button1InternalName As String = "Button1InternalName"
Const button1Sharedvariable As String = "Button1InternalName"

Private Sub TryDeleteButton()
	If SharedVariable.Exists(button1Sharedvariable) Then
		Try
			Dim button1Def As ButtonDefinition = SharedVariable.Value(button1Sharedvariable)
			button1Def.Delete()
		Catch
			'Ignore errors
		End Try
	End If
End Sub


Private Sub CreateButton()

	Dim button1Def As ButtonDefinition =
		ThisApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
			"Button1",
			button1InternalName,
			CommandTypesEnum.kEditMaskCmdType,
			ClientId,
			"Description of Button1",
			"ToolTip of Button1",
			Nothing, 'Small icon
			Nothing, 'Large icon
			ButtonDisplayEnum.kAlwaysDisplayText
			)

	'Add button click handler
	AddHandler button1Def.OnExecute, AddressOf Button1_OnExecute

	'Store button reference in SharedVariable
	SharedVariable.Value(button1Sharedvariable) = button1Def

	'The most simple way, how to add button to Ribbon to AddIns tab
	button1Def.AutoAddToGUI()
End Sub

Private Sub Button1_OnExecute(context As NameValueMap)
	'Implement what happens when Button1 was clicked
	MsgBox("Button 1 clicked")
End Sub