PDA

View Full Version : Intercepting commands in Word 2007



Nelviticus
09-10-2007, 08:15 AM
You can intercept Word commands by writing a function with the proper name, so to intercept File->Save you'd write a function called FileSave. Any time the user clicked the Save button or went to the File menu and clicked Save, your code would run instead of the default action.

Although Word 2007 has completely different menus to previous versions things like FileSave still work. However one important function has changed and I am desparate to find out what the new name is!

In previous versions you could intercept calls to Tools->Unprotect Document with a function called ToolsUnProtectDocument but this just doesn't work in Word 2007 :(

If anyone can figure out what a function should be called to intercept the new 'Stop Protection' button (and it's not StopProtection or ToolsStopProtection) I'd be very, very grateful!

Thanks

TonyJollans
09-10-2007, 09:44 AM
The short answer is that you can't.

Task Panes and VBA don't really interact. The Task Pane for Protection was introduced in Word 2003 and there was no way to trap pressing the Stop Protection... button then either.

From a quick check what appears to happen is that the menu command works (in 2003 and 2007) ... until you add a ToolsUnProtectDocument routine which then does its stuff after which the Task Pane is displayed without the unprotection having happened so you then have to press the button. Seems a bit odd to me but I've never wanted to trap it before - I would use a password if I wanted any control.

Nelviticus
09-11-2007, 01:37 AM
Thank you Tony. Ah well. I will have to come up with some gnarly solution in that case.

The reason we're trying to do this is that we have a 3rd-party application which creates RTF files from Word templates. We want the output files to be locked so that our users can't edit them but although you can protect RTF files from being edited, you can't give that protection a password (at least not the ones generated by this app). Our solution to date has been to base them on a Word template which intercepts the UnProtect command, thus preventing them from being unlocked by users on our network.

The alternative I've come up with is an Auto macro which unlocks them whenever they're opened then locks them again with a password. It seems to work but isn't as simple as the old method.

Thanks once again for your reply.

Regards