VBA Word: How to customize a right click menu bar(shortcut menu)?

S

sharon3874

I am new in VBA word.

In MS word, I want to customize right click menu bar(shortcut menu bar)
on a Hyperlink. I want to change the menu bar(adding new menu item or
delete menu item) dynamically based on different text of a hyperlink.
How do I do it? For example, the right click menu bar on a hyperlink
with text "a" is different than the right click menu bar on a hyperlink
with text "b".

I already know how to add or delete menu item from the right click menu
bar on a hyperlink. What about displaying it diffrently based on
different text?

Can anyone help?

Thanks.
 
J

Jezebel

Trap the WindowBeforeRightClick event. The Sel argument to the event tells
you what is about to be clicked (ie 'a' or 'b').
 
S

sharon3874

Thank you very much!!! It works!!!

I have another question. If it is "A", I want to add a menu item "add"
to the end of the right click menu bar, if it is "B", I want to add a
menu item "edit" and "remove" to the end of the right click menu bar.
it seems like if I right click on "A", then "B", I will have all menu
items("add", "edit" and "remove"), but I only want "edit" and "remove".
Do I need to delete "add"? is there a better way? Also, if I need to
delete "add", how do I check if "add" menu item exists already?

Thanks again
 
J

Jezebel

First, it's not "the" right-click menu -- there are lots of them, depending
on the selection at the time. To see the list, right-click any existing
toolbar, select Customize, and check 'Shortcut menus' on the Toolbars tab.
The Shortcut menus toolbar, visible only when customizing, gives you access
to the shortcut menus in order to customize them manually.

Rather than adding and removing options each time, it might be easier to add
all the options when your code loads (perhaps when you initialize your class
module), then show/hide the ones you want.

You can retain persistent references to the options that you add --

set mnuAdd = CommandBars.....Add(...)

You can check if a menu contains a given options by examining its Controls
collection. When you add an option you can set its Tag property; you can
then use CommandBars.FindControl(Tag:=...) to retrieve it.


Note that menus are part of Office, not part of Word. They are defined in
the Office library. Menus and toolbars are the same thing. The difference is
only whether the options are shown as trext or as icons.
 
S

sharon3874

Thank you very much again!!!! I really appreciate your help!!!

It is very informative, but I still got problems.
(1). the menu items were displayed, but they showed up even I made the
visibility to false(or enabled to false).
(2). Every time I close and open the doc, menu items keep adding to the
end of the existing menu bar(which includes the menu items that added
last time). The menu item looks like this:
....
....
....
Add Additional Link
Edit Additional Link
Remove Additional Link
Add Additional Link
Edit Additional Link
Remove Additional Link
....
....
....
Actually, I just want them displayed once no matter how many times that
I close and open the doc. I tried to make "Temporary" to true when I
added the menu item, but it didn't seem to work.

Can you help to see my code?

Private Sub Class_Initialize()
Dim myMenuBar As CommandBar
Dim newMenu As CommandBarControl

Set myMenuBar = CommandBars("Hyperlink Context Menu")

Set newMenu = myMenuBar.Controls.Add(Type:=msoControlButton, _
Temporary:=True)

newMenu.Caption = "Add Additional Link"
newMenu.Tag = "add"
newMenu.OnAction = "AddAdditionalLinks"

Set newMenu = myMenuBar.Controls.Add(Type:=msoControlButton, _
Temporary:=True)
newMenu.Caption = "Edit Additional Link"
newMenu.Tag = "edit"
newMenu.OnAction = "AddAdditionalLinks"

Set newMenu = myMenuBar.Controls.Add(Type:=msoControlButton, _
Temporary:=True)
newMenu.Caption = "Remove Additional Link"
newMenu.Tag = "remove"
End Sub


Private Sub appWord_WindowBeforeRightClick(ByVal Sel As Selection,
Cancel As Boolean)
....
....
....
Dim rightClickMenuBar As CommandBar
Dim rightClickMenuItem As CommandBarControl

Set rightClickMenuBar = CommandBars("Hyperlink Context Menu")

If isExists = False Then

Set rightClickMenuItem =
rightClickMenuBar.FindControl(Tag:="edit")
rightClickMenuItem.Visible = False

Set rightClickMenuItem =
rightClickMenuBar.FindControl(Tag:="remove")
rightClickMenuItem.Visible = False

Else
Set rightClickMenuItem =
rightClickMenuBar.FindControl(Tag:="add")
rightClickMenuItem.Visible = False

End If
....
....
....
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