Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    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

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You could hook into the Document.SelectionChange Event (see http://msdn.microsoft.com/en-us/libr...ionchange.aspx), but that won't capture menu/ribbon activity.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is compatible with Word integrated VBA?

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It is Word vba. I don't know how it could get more 'integrated' than that.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

    Private Sub DocumentSelectionChange()
    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.Activ eDocument)
    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


    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

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  7. #7
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Great thanks, Paul!


    -avraham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •