Application events for specific template

G

Gordon

This is a follow up to a previous posting from a few days
ago. I've narrowed down my problem but can't quite figure
it out.

I am trying to monitor application events affecting
documents based on a specific template. My template has
form fields and I want to be sure that a field has a value
before allowing the document to be saved.

To do this, I've borrowed Bill Coan's code from
http://word.mvps.org/FAQs/Customization/ProtectWord2000Plus
HeaderContent.htm

I then added a DocumentBeforeSave procedure.

This works fine if I only have one document open, but I am
getting a sporadic error when I have two documents open,
one based on my template and anther based on Normal.dot.

Occasionaly, out of nowhere, I will get an error: "5941
The requested member of the collection does not exist."
When I debug, it appears that the DocumentBeforeSave
procedure is running for the document that is based on
Normal.dot. This happens without any prompting so I think
it may have something to do with the periodic AutoRecover
save.

How do I ensure that my application events are only being
caught for my template, and not any others?

Here is the code I've placed in the ThisDocument module of
my template.
--

Option Explicit

'reserve memory for an application variable
Private WithEvents wdApp As Word.Application

Private Sub Document_New()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub Document_Open()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As
Document, SaveAsUI As Boolean, Cancel As Boolean)

'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument
Then Exit Sub

'
' Debug stops here
'
If Doc.FormFields("Site").Result = "" Then
MsgBox "Please enter the Site before saving."
Doc.FormFields("Site").Range.Select
Cancel = True
End If
End Sub
 
P

Peter Hewett

Hi Gordon

I've only taken a cursory look at the code. But try changing:
If ActiveDocument.AttachedTemplate <> ThisDocument _
Then Exit Sub

to:
If Document.AttachedTemplate <> ThisDocument _
Then Exit Sub

It's possible to save something other than the ActiveDocument. If this does
not clear up the problem please post again.

HTH + Cheers - Peter
 
J

Jezebel

Not 'Document', but 'Doc' -- the document about to be saved as passed as an
argument to the function:

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As
 
P

Peter Hewett

Hi Gordon

Sorry I just typed this rather than pasted it from the IDE and realised that:
If Document.AttachedTemplate <> ThisDocument _
Then Exit Sub
should read:
If Doc.AttachedTemplate <> ThisDocument _
Then Exit Sub

Cheers - Peter
 
P

Peter Hewett

Hi Gordon

This is an after thought about event handling code in templates (rather
than Add-Ins). Just beware that if you have multiple documents open based
of the same template (with Word application event handlers instantiated)
the event handler will be called as many times as there are documents
open!!!

You need to consider this if the event handler does something to the
document you don't want repeated, otherwise you'll get strange results.

HTH + Cheers - Peter
 
T

Tim Ferguson

If Doc.AttachedTemplate <> ThisDocument _
Then Exit Sub

Surely you use Is rather than = or <> to see if they are the same object:

If Not Doc.AttachedTemplate Is ThisDocument Then
Exit Sub
End If

The equal/ not equal operators will work on the default property of the
objects in question and I am not sure what the default property of the
Document object is...

HTH


Tim F
 
P

Peter Hewett

Hi Tim

Don't forget what we're really comparing is:
If Doc.AttachedTemplate.Name <> ThisDocument.Name Then

The code:
If Doc.AttachedTemplate <> ThisDocument

is really using the Template objects default property which is Name.
You can't use "Is" in this case because is compares particular instances of
an object. In our case the names may be the same the the object instances
wont.

HTH + Cheers - Peter
 
G

Gordon

Peter,

I apologize for the late reply, but your solution worked
exactly as I needed. Thank you!

Gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top