Using Events with the Application Object

A

AL

To receive event when focus and/or selection changes in a Word table. I used:

Created class:
Public WithEvents App As Word.Application

Wrote event procedure:
Public Sub App_WindowSelectionChange(ByVal Sel As Selection)

End Sub

Connected declared object with application object:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

If I manually run the Register_Event_Handler, the connection is made and the
program works. But it will not work when the document opens. If I run
another macro after I run Register_Event_Handler, the connection is lost and
I do not receive the event. What can I do to have the event active when the
document opens and stay active? Thanks.
 
J

Jay Freedman

First, store the template containing this code in the
%appdata%\Microsoft\Word\Startup folder so it will be loaded as a global add-in.

Second, rename your Register_Event_Handler macro as AutoExec so it will run when
the add-in is loaded during Word's startup sequence.

Those are the only differences I see between what you've described and the steps
in http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
A

AL

Jay:
Thanks for your response. Your answer would be fine if my file was only
going to be used by myself. However, others will use it too, so I would have
to provide them the add-in as well. I would prefer that it be self-contained
within the file so that anyone could copy the file and use it without further
impact. Is there any way to do that?

AL
 
J

Jay Freedman

With some caveats (to follow), you can do it.

Put all the code into the document. Rename the Register_Event_Handler macro
as AutoOpen. I've tried this, and it works. The App_WindowSelectionChange
procedure continues to execute after other macros run.

Now the caveats:

- Having any macro code in a document, rather than in a template in a
trusted location, will trigger the macro security mechanism. If the user's
security level is set to High, the macros won't run (unless you sign them
with a digital certificate); there will be a message box saying macros are
disabled, and the Control Toolbox will display in design mode (a stupid
side-effect). If the level is set to Medium, the user will be asked whether
to disable or enable the macros.

- The App_WindowSelectionChange procedure will execute when the selection
changes in *every* open document as long as your document is open. To
sidestep this, the first thing in that procedure should be a test of whether
the active document is your document; if not, it should immediately do an
Exit Sub. How you identify "your document" is up to you; I'd suggest saving
a document variable in the original with some unique value such as a GUID,
and testing for the presence of that variable.
 
A

AL

I am okay with both caveats, just glad there aren't any more.

I tried the AutoOpen strategy but it did not work for me. The only way that
it does work is when I have the code (AutoOpen, formerly
Register_Event_Handler) up, select the macro, and then click on the execute
arrow button. If I then force the Document_Open macro to run (it displays a
dialog box for options), AutoOpen no longer executes - at least until I
manually execute it again. Also if I save my file, exit Word, and then bring
my file back up, AutoOpen does execute. However, the Document_Open macro
then runs and somehow my event handler does not run anymore. Do you have any
clues as to what is deactivating the even handler? Does Document_Open turn
anything off when it executes? Regardless, thanks for your for help.
 
J

Jay Freedman

It would have been nice to mention before that the "other macro" was
Document_Open. :-( You should use AutoOpen or Document_Open, but not both.

Move the line

Set X.App = Word.Application

into the Document_Open macro, before the code you already have there, and
move the

Dim X As New EventClassModule

to the top of the ThisDocument module. Delete the AutoOpen macro and the Dim
X statement from the regular module.
 
A

AL

Jay:
My apologies, I should have realized not to use both at the same time.
Frustration got in the way.

During my previous attempts to get this function to work I had also tried
moving the "Set X.App = Word.Application" to Document_Open, but it did not
work. The "Dim X as New EventClassModule" has always been at the top of the
ThisDocument module.

I tried again, getting rid of AutoOpen, but am still unsuccessful. I will
continue to plug away at this. I appreciate your help and patience. Thanks.

AL
 
A

AL

Jay:
I finally figured out what my problem was. Your assistance was correct, but
my program had a bug I was previously unaware of. Elsewhere in my code I had
an "End" statement instead of "Exit Sub". While it did not exhibit any
problem before I tried to incorporate the event handler, it kills the event
handler connection. I discovered my bug when I stepped through the
execution. Again, thanks for your help and patience.
 
J

Jay Freedman

Excellent! Glad to help.

My next suggestion was going to be single-stepping to see at what point
things went wrong. It's good to know that would have been the right advice.
 

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