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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

sharon3874

Programmer
Oct 27, 2006
24
US
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.
 

I don't think there's any way you can customize the menus directly at that level of granularity but you should be able to do something in the WindowBeforeRightClick Application Event.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
yes, I tried WindowBeforeRightClick event, and it worked. I really appreciate your help
 
Thank you. However, I still got problems.
(1). I was able to add customized menu items to the shortcut menu bar, but I couldn't hide some of them 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
 

Before I look more closely, what version of Word are you using? Temporary:=True doesn't work properly in Word 2000.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hmmm...

I thought that bug was fixed but I see the same problem in 2003 as you describe so it looks like you'll have to delete the controls yourself - in the class terminate routine perhaps. When you do that it should solve your other problems but you will have to tidy up the commandbar first to remove all the extra entries - probably quite a few if you've been testing this.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top