Hi all,
Is there any way I can use the Word VBA environment to capture all mouse clicks within a document?
Result would be a selection or a character # within the doc.
TIA
-avraham
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Word VBA 2003/07/10: to capture all mouse clicks within the document?
You could hook into the Document.SelectionChange Event (see http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.selectionchange.aspx), but that won’t capture menu/ribbon activity.
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi Paul,
Great thanks for the response.
Sorry it took a while for me to get back to you.
I copy+pasted the following code into the Word VBA environment.
[INDENT]
Private Sub DocumentSelectionChange()
Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
AddHandler vstoDoc.SelectionChange, AddressOf ThisDocument_SelectionChange
End Sub
Private Sub ThisDocument_SelectionChange(ByVal sender As Object, ByVal e As Microsoft.Office. _
Tools.Word.SelectionEventArgs)
System.Windows.Forms.MessageBox.Show (“The selection in the document has changed.”)
End Sub
[/INDENT]
The line starting with “Dim vstoDoc As Document” is flagged by the VGA environment as an error.
Do I need to add a reference to accept the Globals.Factory.GetVstoObject() declaration?
Thanks,
Avraham
Hi Avraham,
The code in the link I gave you is for Visual Studio’s implementation, which may have misled you. Sorry ’bout that.
Using Events requires that you instantiate the app object class. To do this, create a new class module to register the desired Word events. In that module put:
Option Explicit Public WithEvents wdApp As Word.Application
Below that, add the following code to intercept the change event:
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection) If Sel.Information(wdWithInTable) Then MsgBox "The table selection in the document has changed." Else MsgBox "The selection in the document is not in a table." End If End Sub
The above shows how you can capture not only the fact of the event but also where is occurred.
Then, in the document’s ‘ThisDocument’ module, put:
Option Explicit Dim wdAppClass As New ThisApplication Private Sub Document_Open() Set wdAppClass.wdApp = Word.Application End Sub
This code will allow the events in your Word document to be called.
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi,
I am wanting to use this technique in a Word 2003 document.
I have copy & pasted the vba into the relevant modules, but the compiler throws an error: “User-defined type not defined” when it encounters the line “Dim wdAppClass As New ThisApplication”.
Can you (or anyone) assist?
(I wonder if there may be a typo in the original.)
Cheers.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.