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

adding to the mouse right-click menu 1

Status
Not open for further replies.

PeterJ01

Technical User
Jul 9, 2009
10
AU
I wish to run a macro in Word by right clicking the mouse, and then slelcting the macro from the drop down menu.

Canb anyonme please advise how to do this?

regards,
Peter
 
I don't believe that's naively possible. If anything, it'd probably have to be a registry hack.

What you can do is assign a button and/or keyboard shortcut to a macro.

--

"If to err is human, then I must be some kind of human!" -Me
 
(assuming you are _not_ in Word 2007) ...

Tools > Customize ...
In the Toolbars tab, scroll down and check "Shortcut menus"

This will open up a new toolbar giving you access to all the shortcut menus - customise them the same way you would any other toolbar. The tricky bit, of course, is deciding which toolbars you want to add your macro to.

Enjoy,
Tony

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

I'm working (slowly) on my own website
 
Good info there, Tony.

So, what if you're in Word 2007? Is there a way to do it there?

--

"If to err is human, then I must be some kind of human!" -Me
 
You can pick mouse right-click for the whole application. A starting point (one document track):
Code:
Public WithEvents wdApp As Word.Application

Private Sub Document_Close()
Set wdApp = Nothing
End Sub

Private Sub Document_Open()
Set wdApp = Application
End Sub

Private Sub wdApp_WindowBeforeRightClick(ByVal Sel As Selection, Cancel As Boolean)
If Sel.Document Is ThisDocument Then
    Cancel = True
    MsgBox Sel.Text
End If
End Sub

combo
 
I think what you might have been missing in your search is the name of the "drop down menu" when you right click.... That's called the Context Menu.

I've tried a few googles for running a macro from context menu and came up with a few promising hits. There was a reply to another question on another board with a couple of links to free utilities that helped to do this, but the links are dead.

Then there was this. I don't have time to look over it in detail, but check it out.

kjv1611 said:
I don't believe that's naively possible.
Perhaps not naively, but I'm pretty world-weary. [wink]

TonyJollans said:
The tricky bit, of course, is deciding which toolbars you want to add your macro to.
I don't use word enough for this to be worth my time, but in pre-2007 Excel versions, I avoided that by having an entire custom toolbar full of custom buttons.

Alas, in 2007 it's not so easy. Now I have custom drop-down menus instead.

[tt][blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
[gray](note to self: read replies before hitting submit)[/gray]

[tt][blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
kjv said:
So, what if you're in Word 2007? Is there a way to do it there?

You can do some customisation through VBA but there is no UI for it.

Enjoy,
Tony

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

I'm working (slowly) on my own website
 
The tricky bit, of course, is deciding which toolbars you want to add your macro to. "

[lol]

In other words, you could add it to a right-click context menu that will show it under X condition...but it will NOT show it as a right click context menu item under Y condition.

And there are a LOT of conditions.

combo:
Code:
If Sel.Document Is ThisDocument
As Sel is declared as Selection, could it EVER not be ThisDocument?

Can Selection ever be in another document from ThisDocument? Is not Selection defined as the cursor location in the ActiveDocument...thus ThisDocument?

I am not saying you are incorrect, as my brain is definitely off. Just trying to clarify.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Can Selection ever be in another document from ThisDocument? Is not Selection defined as the cursor location in the ActiveDocument...thus ThisDocument?

I am not saying you are incorrect, as my brain is definitely off.

Your brain is definitely off [lol]

Take care, Gerry. I'm sure I don't really need to explain the difference between ActiveDocument and ThisDocument to you.

Enjoy,
Tony

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

I'm working (slowly) on my own website
 
Fumei,
as the code picks application level event, Sel can be anywhere, I limited action to document with code.

combo
 
Excuse me, but yes Sel can be anywhere, but again...can Selection.Document ever NOT equal ThisDocument?

Yes Tony, I do know the difference between ActiveDocument and ThisDocument. Although...maybe I don't!

So tell me, is not the Selection object always in ActiveDocument, and thus - in this case - Selection.Document = ThisDocument?

Now if it was Application.ActiveWindow.Document....another story.

Say you have Selection in DocumentA, DocumentA is ActiveDocument. DocumentB is open, so it is in the Documents collection.

However, to right click in DocumentB, you must...make DocumentB active, thus Selection is now in DocumentB. Selection.Document is ThisDocument.

The only time I can see that Selection.Document <> ThisDocument is:

The Selection is in DocumentA, BUT you execute a procedure from DocumentB. Not a right click, which again means the Selection is IN DocumentB.


"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Create a document containing the code - call it Doc A.
Create another document - call it Doc B.
Close both documents.
Open Doc A (this will initialise the event trap)
Open Doc B
Right click in Doc B
The Selection (Sel) is in Doc B - Sel.Document is Doc B
(Doc B is also the ActiveDocument - but it's not relevant)
Doc A (where the code is) will be ThisDocument
You will get a normal right click menu because the check fails
Switch to, and right click in, Doc A
You will get the message box because the check passes.


Or ... what if the code was in a Template? The template will be ThisDocument and it won't have a Window or a Selection at all.


Enjoy,
Tony

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

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top