Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    Help!!!

    I have a word template that I have created for official company documents. It contains multiple sections of boilerplate text that should not be changed (as per company and regulatory guidelines.) The authoring community was upset that the initial version of this template, which used protected sections and document protection, precluded them from using Word's Track Changes feature (or at least precludes them from accepting or rejecting tracked changes, because of the document protection.)

    I'm trying to devise an alternative that hijacks certain commands, such as Cut, Copy, or Paste, that would otherwise alter or delete these sections. That approach works for those methods by intercepting the action and checking to see if it's being performed on a particular section or bookmark. However, what I also really want is to check when they simply click in or edit a certain section of the document.

    Ostensibly, I'd like to trap a Click or Change event and alert the user that they are editing "protected" areas of the document. (I would do this either by creating protected sections or by creating bookmarks and comparing the Selection/range to it.) Is there actually any way to do this in a Word document using VBA? As I said, I can't just use "Forms", because that type of protection would prevent them from accepting changes. I also can't simply replace all of the bookmarked sections at the Save (or some such) event, because there may have been a legitimate change to that area, and I wouldn't want to overwrite it.

    Has anyone had any experience with anything like this? Can it be done? Might I consider some other approach? (This is very important, because you KNOW you can't trust those authors to leave standard text alone!)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    In <post#=342,898>post 342,898</post: >, you'll find a method to protect the document when the user enters a protected section, and to unprotect the document when the user is in an unprotected section. It's still risky: the user can press Ctrl+A while in an unprotected section and delete everything, for instance. You could hijack EditSelectAll, I suppose.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    Hans... as always, you rock! A little bit of tweaking, some commands to deal with Tracked Changes in protected sections, and the users will be much obliged.

    Oh, and hijacking the EditSelectAll command has me almost there. The only thing I can't do is trap the "triple-click" in the left margin; that doesn't respond as a Select All. Any ideas?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    Hans, I seem to have a problem implementing the code somehow... it seems that, when I have a document open that has the feature implemented, it affects all other documents that I open as well. Currently, I'm working around it by checking to see if one of the document's properties is set to a certain value, but that's just fortuitous. Is there something I'm missing? Why does it seem to apply to all open documents?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    The code uses an application-level event handler, so it will affect all open documents. To avoid that, you could check in the app_WindowSelectionChange procedure whether ActiveDocument.AttachedTemplate is your template, and exit if it isn't.

  6. #6
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    Hans-
    Now that I have that sweet piece of code running in my documents, I've been asked to add to it.
    There are users that legitimately need to access and edit the sections of the document that get protected "in real time." So, what they want is a command or menu option that they can select that will disable the WindowSelectionChange event that I hijacked. It wouldn't need to be re-enabled, since they would finish their job and close the file, and the next time it's opened the feature would kick in again. The "disable" feature would only be available on a special menu (which I have already programmed) that is only visible to these "legitimate" users, so I'm not worried about keeping it out of the hands of others.
    Can this be done through code? Thanks for all of your advice and help with this.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Trapping Mouse-Clicks and Keystrokes in Word (Word XP VBA)

    This is off the top of my head, I haven't actually tested it.

    - Declare a public variable blnDisableEvent in a standard module in your template:

    Public blnDisableEvent As Boolean

    - Check this variable at the beginning of the WindowSelectionChange event procedure:

    If blnDisableEvent = True Then Exit Sub

    - Use the OnAction macro of the special menu option to set the variable to True:

    blnDisableEvent = True

    - You state that it's not necessary to re-enable the WindowSelectionChange event, but if you ever need it, you can use another menu option to set the variable to False:

    blnDisableEvent = False

Posting Permissions

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