problem with custom menu in MS Word

V

vlad

I'm adding a menu item using the code below. This code is placed in a .dot
file in Word StartUp folder.
So I have some questions.
1. What's CustomizationContext for? Notice the menu is created and cleared
in different those (this appeared to work best).
It looks like menu MUST be cleared in a NormalTemplate while it's created in
ActiveDocument.
2. The first time I click my menu I get the error like "Macro not found".
The second time, third one and so on it works OK.

May be anyone knows what should I write in my code to:
1. Create menu from the .dot file in Word StartUp folder.
2. Menu shold be cleared when Word exits.
3. Menu should run a macro every time it's clicked, not just second try.

Please, help!

Public Sub AddSaveToWFMenu()
Dim FileMenu As CommandBarPopup
Dim MyControl As CommandBarControl

On Error Resume Next
CustomizationContext = ActiveDocument
Set FileMenu = CommandBars("Menu Bar").Controls(1)
Set MyControl = FileMenu.Controls.Add(Type:=msoControlButton, Before:=4,
Temporary:=True)
MyControl.Caption = "Custom menu Action"
MyControl.OnAction = "SaveToWF"
End Sub

Public Sub SaveToWF()
' do some job
msgbox "success!"
End Sub

Public Sub DeleteSaveToWFMenu()
Dim FileMenu As CommandBarPopup

'On Error Resume Next
CustomizationContext = NormalTemplate
Set FileMenu = CommandBars("Menu Bar").Controls(1)
FileMenu.Reset

End Sub

Public Sub AutoExec()
AddSaveToWFMenu
End Sub

Public Sub AutoExit()
DeleteSaveToWFMenu
End Sub
 

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