Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a menu button to run VBA app 2

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
0
36
US
I created VBA program now how do I make a menu button to launch it. instead of clicking Tools, Macros etc etc

DougP
[r2d2] < I love mine
 
Here's a way to do it entirely thru VBA code.

Put the following code into its own code module, and run the "Menu main" command. It creates a new, top level menu, and 4 sub menus. You'll want to change the name of the Main menu ("My&Menu"), the name(s) of the Sub menus("My&SubMenu"), and the names of the subroutine(s) called "TestSub" to the names of your own subroutines, to fit your application.

' ---- snip ----- snip ---- snip ----- snip ----

Sub MenuMain()

Dim retMenu As AcadPopupMenu
Dim retMenuItem As AcadPopupMenuItem

Const MainMenuName As String = "My&Menu" ' add an Ampersand in front of letter to make it a Hotkey
Const SubMenuName As String = "My&SubMenu"


' either add new, or return existing main menu Item
Set retMenu = AddMainMenu(MainMenuName) '' add (or GET) main menu item

' add some sub menu item to our main menu

For I% = 1 To 4
Set retMenuItem = AddMainMenuItem(retMenu, SubMenuName & Str$(I%), "TestSub")
Next '



End Sub


Private Function AddMainMenu(strMenuName As String) As AcadPopupMenu
' adds a main menu to acad menus, or returns an existing menu with the same name

Dim currMenuGroup As AcadMenuGroup

Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item("ACAD")
For I = 0 To currMenuGroup.Menus.Count - 1
If currMenuGroup.Menus(I).Name = strMenuName Then
Set AddMainMenu = currMenuGroup.Menus(I)
Exit Function
End If
Next


' if we're still here, we didnt find the menu, so we'll add one
Set AddMainMenu = currMenuGroup.Menus.Add(strMenuName)
' Display the menu on the menu bar
AddMainMenu.InsertInMenuBar (ThisDrawing.Application.MenuBar.Count + 1)


End Function


Private Function AddMainMenuItem(objMenu As AcadPopupMenu, strMenuItem As String, strMacroName As String) As AcadPopupMenuItem

' adds a sub menu item to the passed menu object
' the "strMenuIte" param is the name of ther menu, per VB xconvention, embed an ampersand "&"
' before the letter you want to be a hotkey
' The "strMacroName" is the name of the Subroutine you want called when the menu is selected

Dim openMacro As String

openMacro = "-VBARUN " & strMacroName & " " ' add a space to enmnu item to emulate the ENTER key]'
Set AddMainMenuItem = objMenu.AddMenuItem(objMenu.Count + 1, strMenuItem, openMacro)

End Function


Sub TestSub()
' name of routine to call when menu item is selected
MsgBox "your menu was just selected"
End Sub


' ---- snip ----- snip ---- snip ----- snip ----

 
rocheey, thank you
I would rather have a button on a toolbar so I don't have to click a menu or go that far up and then a submenu. I want to just click a button on the side. I have tons of buttons already, some use Lisp most are just ??? commands strung together. With LISP you load it then put the name in the button. VBA does not work like that for some reason. I loaded it but a button with the following says Unknown command.
Code:
^C^C-vbarun;"ListChainCustody";

my VBA dvd is called ListChainCustody.dvd it has a form that pops up if I hit the run button in the VBA IDE.

Also, how do I get rid of the My Menu I just created.

DougP
[r2d2] < I love mine
 
I'm using version 2000i BTW
I saw this is your example above
VBARUN " & strMacroName &
I added it to a button.
^C^C-vbarun ListChainCustody

and when I click it I get this message which I copied from the command line

Command: -vbarun
Macro name: ListChainCustody
Macro not found.
If I click the "tools" menu then "load appication" it show in the Loaded list and history and if I click load it says its already loaded.

DougP
[r2d2] < I love mine
 
Hi DougP,

You need to tell the vbarun functionality where the macro can be found:

Code:
^C^C-vbarun "Module1.ListChainCustody"

or

Code:
^C^C-vbarun "ThisDrawing.ListChainCustody"

etc.

HTH
Todd
 
Thank you it worked. Here is what I had to do...
I created a Module and in it a sub called LoadChainOfCustody

Code:
Sub LoadChainOfCustody()
    'this sub is necessary to load the form 
    frmChainOfCustody.Show
End Sub

Then using your code, my button has this in the “Macro associated with this button:
Code:
^C^C-vbarun "Module1.LoadChainOfCustody"


DougP
[r2d2] < I love mine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top