×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

create a menu button to run VBA app
2

create a menu button to run VBA app

create a menu button to run VBA app

(OP)
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
www.robots2000.com

RE: create a menu button to run VBA app

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

 

RE: create a menu button to run VBA app

(OP)
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
www.robots2000.com

RE: create a menu button to run VBA app

(OP)
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
www.robots2000.com

RE: create a menu button to run VBA app

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

RE: create a menu button to run VBA app

(OP)
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
www.robots2000.com

RE: create a menu button to run VBA app

(OP)
rocheey, that is fantastic code which I may use in the future.

DougP
r2d2 < I love mine
www.robots2000.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close