• Word VBA 2003/07/10: to capture all mouse clicks within the document?

    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?

    Author
    Topic
    #485960

    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

    Viewing 5 reply threads
    Author
    Replies
    • #1352919

      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]

    • #1352941

      This is compatible with Word integrated VBA?

    • #1352953

      It is Word vba. I don’t know how it could get more ‘integrated’ than that.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1355631

      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

    • #1355721

      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:

      Code:
      Option Explicit
      Public WithEvents wdApp As Word.Application

      Below that, add the following code to intercept the change event:

      Code:
      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:

      Code:
      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]

      • #2681395

        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.

    • #1358019

      Great thanks, Paul!

      -avraham

    Viewing 5 reply threads
    Reply To: Word VBA 2003/07/10: to capture all mouse clicks within the document?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: