Application Event Procedure impacting "Normal" documents

J

Jon Borel

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.Applicatio
----------------------------------------------------------------------------------
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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jon Borel

Thanks Jay! (Sorry for the delay; I got sidetracked on another project.)

Your solution works wonderfully. Now though, when I close the second
"normal" doc and return to the custom.dot doc, the eventhandler has been
disabled. I can reinitialize it by manually running the Autoexec macro. Is
there a more graceful/automatic way to reenable the event handler?

Jon
 
J

Jay Freedman

You've succeeded in confusing me. :) Exactly where did you insert the
suggested code?

I should have been more explicit; I meant for you to put the code into the
beginning of the AppName_WindowSelectionChange procedure. That's the "event
handler" I was referring to. It sounds like you put it somewhere else.

The idea is not to disable the SelectionChange code, but simply to have it
"do nothing" (other than the check of which template is attached to the
active document) when the document isn't the right one.
 
J

Jon Borel

Hi Jay,

Wow! Now I've gone and confused myself too... it seems to be working
correctly, just as you expected it to. I don't know why it didn't before,
maybe I wasn't holding my head right...

Thank you for the fast and thorough solution.

Jon
 

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