Group:  Microsoft Word ยป microsoft.public.word.vba.general
Thread: Application Event Procedure impacting "Normal" documents

Application Event Procedure impacting "Normal" documents
Jon Borel 12/31/2008 7:43:01 PM
Hello,

In my custom Word 2003 template, I have the following application event
procedure to check the style at the current insertion point (or selection):

Public WithEvents AppThatLooksInsideThisEventHandler As Word.Application ----------------------------------------------------------------------------------
Private Sub AppName_WindowSelectionChange(ByVal Sel As Selection)

If Selection.Type = wdSelectionNormal Or _
Selection.Type = wdSelectionIP Then

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
Do this
Else
Do This
End If

End If

End Sub

It works great UNTIL I open a new "Normal" document while I have a document
based on this custom template open. Then, the event handler run-time errors
with "The requested member of the collection does not exist" and debug takes
me to the line:

If Selection.Style = ActiveDocument.Styles("Bold Word")

I'm pretty sure this is because the new "Normal" document doesn't include
the "Bold Word" style. (I'm surprised the Else condition doesn't handle
this.) So, I need a way to tell VBA to only run the event handler for
documents based on the (current) custom template, not for (external) "Normal"
documents.

Thanks in advance for your consideration,
Jon
Re: Application Event Procedure impacting "Normal" documents
Jay Freedman <jay.freedman[ at ]verizon.net> 12/31/2008 10:42:39 PM
Hi Jon,

When more than one document is open, and one of them has implemented an
application event handler, there is no way to prevent the handler from firing in
all documents (that's what "application event" means). However, you can include
code at the start of the event handler that exits immediately if the current
document isn't based on your custom template:

If ActiveDocument.AttachedTemplate <> "custom.dot" Then
Exit Sub
End If

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Wed, 31 Dec 2008 11:43:01 -0800, Jon Borel
<JonBorel[ at ]discussions.microsoft.com> wrote:

[Quoted Text]
>Hello,
>
>In my custom Word 2003 template, I have the following application event
>procedure to check the style at the current insertion point (or selection):
>
>Public WithEvents AppThatLooksInsideThisEventHandler As Word.Application
>----------------------------------------------------------------------------------
>Private Sub AppName_WindowSelectionChange(ByVal Sel As Selection)
>
> If Selection.Type = wdSelectionNormal Or _
> Selection.Type = wdSelectionIP Then
>
> If Selection.Style = ActiveDocument.Styles("Bold Word") Then
> Do this
> Else
> Do This
> End If
>
> End If
>
>End Sub
>
>It works great UNTIL I open a new "Normal" document while I have a document
>based on this custom template open. Then, the event handler run-time errors
>with "The requested member of the collection does not exist" and debug takes
>me to the line:
>
>If Selection.Style = ActiveDocument.Styles("Bold Word")
>
>I'm pretty sure this is because the new "Normal" document doesn't include
>the "Bold Word" style. (I'm surprised the Else condition doesn't handle
>this.) So, I need a way to tell VBA to only run the event handler for
>documents based on the (current) custom template, not for (external) "Normal"
>documents.
>
>Thanks in advance for your consideration,
>Jon

Home | Search | Terms | Imprint
Newsgroups Reader