PDA

View Full Version : Class commands running in all documents



dewgs
06-29-2010, 05:09 AM
Hi all,

I am at a loss and have only recently ventured into using classes within my vba apps. I have a bunch of code that need to run on WindowActivate or doc change events to ensure my custom toolbar remains visible whenever my document has focus (another addin that must be installed hides my toolbar when my doc loses focus).

My question is, can I restrict the code in the eventclassmodule to only apply to my document? As it is in the example below, once the eventclassmodule is intialised, the msg "beta" will appear whenever any document is opened, new or is activated. I would like the code to run only when my document is activated, not other documents.

code from EventClassModuleCMPS class


Public WithEvents App As Word.Application

Private Sub app_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)

MsgBox "beta"

end sub


code from the loadclasscmps module

Public X As New EventClassModuleCMPS

Sub RegisterEventHandler()
Set X.App = Word.Application

End Sub

thanks

EDIT* Also, if anyone else can think of a way to automatically .enabled .visible my toolbar when my document has focus (so to speak) without using ActiveWindow or DocumentChange events, I'm open to suggestions.

fumei
06-29-2010, 09:34 AM
I am curious as to WHY you want to do this. Why do you care if an unactive document has a particular, well, anything or not. Seems to be an overhead of processing for questionable results.

However, to answer your actual question (I think), yes, the event class would execute Msgbox "beta" for all documents. This is as it should be.

So................

1. give your docuemnt (whatever that is) an object name.
2. in the Activate event, test to see if the ActiveDocument is that object.

If it is, do whatever.
If it is not, do not do whatever.