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
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