Create Custom Menu with VBA

K

Kevin R

I want to create a custom menu with options that trigger macros using vba that will show up at the end of the Standard toolbar (after Help) in Word. I can do this with View | Toolbars | Customize but really want to learn how using vba instead. This will be saved in a dot file in the startup folder so that it appears automatically when Word is opened. All my macros are listed in a module in the dot file. I've read all the informaton on the internet on how to do this and am more confused now than ever. I need help step by step, beginning to end in a hurry! Thanks.
 
T

Tarjei T. Jensen

Kevin R said:
I want to create a custom menu with options that trigger macros
using vba that will show up at the end of the Standard toolbar
(after Help) in Word. I can do this with View | Toolbars | Customize
but really want to learn how using vba instead. This will be saved
in a dot file in the startup folder so that it appears automatically
when Word is opened. All my macros are listed in a module in
the dot file. I've read all the informaton on the internet on how to
do this and am more confused now than ever. I need help step
by step, beginning to end in a hurry! Thanks.

Below is the content of ThisDocument in a test template I'm using. It is NOT
fully developed. It is work in progress. I lifted the important bits from
http://word.mvps.org/FAQs/MacrosVBA/AddMenu.htm

The single menu item opens an userform. Hence you will have to do your own
stuff inside show_language_listbox.

Create the menu by typing "call ThisDocument.set_menu" in the immediate
window. Each time you call this function it will create a new menu. With the
same name.

Delete the menu with the command CommandBars("Menu
Bar").Controls("My-menu").delete
Repeat as often as neccessary.

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ThisDocument"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True

Option Explicit

Public Sub set_menu()
Dim mymenu As CommandBarControl
Dim mybutton As CommandBarButton
Dim myCaption As String

myCaption = "My-menu"

Debug.Print "enter set_menu"

'specify where new menu will be stored
CustomizationContext = ActiveDocument.AttachedTemplate

If Not Menu_Bar_menu_exists(myCaption) Then
Debug.Print "set_menu: menu not present"
End If

'add a menu to the Menu Bar
Set mymenu = CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup,
temporary:=False)
With mymenu
.Caption = myCaption
.Tag = "MyTag"
.TooltipText = "Word utilities provided by My"
End With

'add commands to the new menu
With CommandBars("Menu Bar").Controls(myCaption).Controls

Set mybutton = .Add(Type:=msoControlButton)
mybutton.Caption = "Change language and company"
mybutton.OnAction = "TestMacro"


End With

ThisDocument.Saved = True ' Keep users from getting annoying messages
Debug.Print "exit set_menu"
End Sub


Function GetControlNames(barname)
Dim cbarMenu As CommandBar
Dim cbarControl As CommandBarControl
Set cbarMenu = CommandBars(barname)
Debug.Print cbarMenu.Controls.Count
For Each cbarControl In cbarMenu.Controls
Debug.Print cbarControl.Caption; _
vbTab; cbarControl.Enabled; _
vbTab; cbarControl.Type; _
vbTab; cbarControl.ID
Next
End Function


Public Sub testmacro()
Debug.Print "enter testmacro"
Call show_language_listbox
Debug.Print "exit testmacro"
End Sub



Public Sub show_language_listbox()
' your stuff here
End Sub

Public Function Menu_Bar_menu_exists(ByVal menu_name As String)
Dim thisMenu As CommandBarControl

On Error Resume Next
Menu_Bar_menu_exists = True
Set thisMenu = CommandBars("Menu Bar").Controls(menu_name)

If thisMenu Is Nothing Then
Menu_Bar_menu_exists = False
End If
On Error GoTo 0

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top