Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intercepting Events in Word

Status
Not open for further replies.

jpadie

Technical User
Nov 24, 2003
10,094
0
0
FR
Hi

I have not developed in VBA for many years. and i find myself struggling somewhat!

at a high level I'm just wanting to explore the event model that will work across multiple versions of Word. i.e. Word for Mac 2011 and Word 2003 and later.

at a more detailed level, I am building a tool for a legal client to distribute to their internal users that needs to expire every (say) quarter to ensure that it is refreshed formally. the expiry should stop saving documents, printing them and creating new versions based on a template. but not opening them. The expiry should have an override to allow one of the legal team to extend the expiry of a particular document. This is a crude mechanism and I will refine it when I get the basics working.

the expiry date is stored in a customDocumentProperty.

at the moment I can get the menu macros to work but I'm trying to intercept the event model instead as that seems to offer more control.

I have read a bunch of links at mvps and elsewhere.

I currently have the following

In ThisDocument
Code:
Option Explicit
Dim oAppClass As New thisApplication

Sub Register_Event_Handler()
    Set oAppClass.oApp = Word.Application
End Sub

in a class module called thisApplication
Code:
Option Explicit
Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
  MsgBox ("before print")
End Sub

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
  MsgBox ("before save")
End Sub

Private Sub oApp_NewDocument(ByVal Doc As Document)
  MsgBox ("new document")
End Sub

Private Sub oApp_DocumentChange()
  MsgBox ("document change")
End Sub

I'm not getting any error messages; just no message boxes.

any pointers greatly appreciated.

An anodyne dotm file is attached if that helps (although I appreciate that noone may want to run a 'foreign' macro on their machine.
 
Did you call the Register_Event_Handler procedure ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
@PHV
I am not sure what you mean by that? if you mean by placing the cursor in the subroutine and clicking run, then Yes.

should it instead be in some auto_exec type subroutine? i had assumed (perhaps wrongly) that the register_event_handler function name was somehow a 'magic' name that told word to register event handlers.
 
Thanks macropod
I have read that page countless times over the last week. The solution that they refer to involves using an addin which would not be suitable for this scale of deployment across organisations.
Also autoexec triggers at app start. This set of code needs to run at the document start point.
I got the register event handler code from a Microsoft web site (ms published code not user contributed)
Perhaps autopen would be better but it must not be capable of override via shift so is not really suitable for this purpose.
 
it must not be capable of override via shift so is not really suitable for this purpose.
So how do you propose preventing users from disabling your event handler? As it seems you're already aware, they can do that by starting Word in safe mode. They can also:
• copy the document and open it on a system that lacks your template
• change the attached template
• copy/paste the content to another document based on a different template
• disable macros
etc. As for:
The solution that they refer to involves using an addin which would not be suitable for this scale of deployment across organisations.
But an addin is precisely what you're developing.

Cheers
Paul Edstein
[MS MVP - Word]
 
Have you considered Rights Management as n alternative solution?
 
oh dear. i may be barking up the wrong tree altogether then

1. i had assumed that the macros would follow the document. so the controls present in the template would exist in all documents created under that template, and so would survive if the document were sent by email to a third party etc. Is this not the case?

2. add-in - by that I meant a file that stood aside and was brought in by reference. point 1 was what i was trying to achieve.

3. the actual template will be locked. they would need passwords to be able to change it. hackable but an acceptable risk.

4. disabling macros would render the document useless as it will be locked to form fields only and only the macros will allow (e.g.) extra table rows to be added.

5. copy and paste would also render the doc useless. or at least less useful.

I will take a look at Rights Management but at first glance it is not supported below Office 2010 and would require all recipients of the document in/out of organisation to install the rights management client. Which wouldn't be an acceptable solution.

thanks for your help. I will try and get the macros themselves to work and see where I can go from there.
 
1. i had assumed that the macros would follow the document. so the controls present in the template would exist in all documents created under that template, and so would survive if the document were sent by email to a third party etc. Is this not the case?
No, that is not the case. the code remains in the template, not in documents created from it. So, if you send the document to someone else without the template, they won't be able to use the associated code.

Cheers
Paul Edstein
[MS MVP - Word]
 
damn. then i need to create the template as an ordinary document and have them duplicate it.

thanks. that seems to be an unfortunate limitation. perhaps i can use the template to write code to the document. thus limiting the 'creation' correctly whilst not the use of the document once created.

 
Rights Management is supported from Office 2003 onwards.and the client is included from Vista onwards.
 
Thanks strongm.
It seems that's not the case at the organisation where I am currently working. Although perhaps it is and no one uses it. They do have oracle irm though. Definitely macros are a better solution for this. I will keep trying.
 
Fair enough. It is just that your detailed description of what you are trying to achieve is exactly the sort of thing that Microsoft's rights management is designed to do, which is why I suggested it.
 
I agree.

Although it is more that the document shouldn't be usable after a certain period. It should still be viewable. I will dig further into rights management on a test machine. Interested to know how it is limited viz auth. Whether there has to be AD integration etc.

But I'm annoyed by the code itself not working so I can't let that drop or my ocd will go mad! If only you could write macros in php ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top