Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sharon3874 - 27 Oct 2006 22:45 GMT
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.
Jezebel - 27 Oct 2006 23:04 GMT
Trap the WindowBeforeRightClick event. The Sel argument to the event tells
you what is about to be clicked (ie 'a' or 'b').

>I am new in VBA word.
>
[quoted text clipped - 12 lines]
>
> Thanks.
sharon3874 - 28 Oct 2006 01:33 GMT
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
Jezebel - 28 Oct 2006 03:32 GMT
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.

> Thank you very much!!! It works!!!
>
[quoted text clipped - 7 lines]
>
> Thanks again
sharon3874 - 30 Oct 2006 20:34 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.