Document_Open

B

Bruce Maston

As I understand it, if you put Document_Open event into
ThisDocument, the procedure runs whenever you open the
document. For me, this does not work and nothing
happens. Even a Msgbox won't pop up. What am I missing
to get something to happen (specifically fill in a
bookmark) when I click to open a Word Document. I'm using
Word 2000.
 
J

Jezebel

Not quite. There are three ways you can trap the open document event. The
simplest is to write a macro called AutoOpen. If you put this in a document,
it will run whenever that document is opened; if you put it in a template,
it will run whenever any document based on that template is opened.

Document_Open is another method: to trap this event you need a 'WithEvents'
reference to the Word application, which has to be in an object code module
(a class module or a form) -- not in an ordinary code module; and to make
that work your main code has to create an instance of the object module.
This is useful if you're running Word via another application, but it's
fairly heavy-handed for a simple 'run this code when the document opens'
task.

The third method is to create a code module called 'AutoOpen' and include a
Sub Main within it.
 
J

JGM

Hi Jezebel,
Document_Open is another method: to trap this event you need a 'WithEvents'
reference to the Word application, which has to be in an object code module
(a class module or a form) -- not in an ordinary code module; and to make
that work your main code has to create an instance of the object module.
This is useful if you're running Word via another application, but it's
fairly heavy-handed for a simple 'run this code when the document opens'
task.

Please, can you clarify this...

I have always used the ThisDocument.Document_Open event and I have never had
any problems...
In fact, I have stopped using the AutoOpen approach over 2 years ago...
If it is not working for Bruuce, then he is doing something wrong or he has
overlooked something simple, or I have been incrdedibly lucky these past 2
years.

I have never included a WithEvents procedure in a class module...
What do you mean by "Heavy-handed"?
I thought that these procedures where included to be used, even with simple
tasks...

I am not criticizing... I want to learn from your point of view on this.

Thanks
 
M

Martin Seelhofer

Hello NG
Please, can you clarify this...
... or I have been incrdedibly lucky these past 2 years.

From my point of view, the "AutoOpen"-approach is deprecated and
should not be used anymore, since it does not follow the more object-
oriented approach of Document_Open. So I don't think you have been
too lucky in the past ;-)

The reason why the Document_Open-Event is not fired in Bruce' case
I suspect to be a security issue (value 'High' in Tools - Macro -
Security?)...

However, the Document_Open-event-handler IS the right way
to do it. In my opinion, that is ;-)
I have never included a WithEvents procedure in a class module...
What do you mean by "Heavy-handed"?

Well, the WithEvents-stuff is a way to attach event handlers to an
object variable. However, you only need this, if your object does
not already have a corresponding class-module attached. Since
there is one (a class module) for the Document-Object (ThisDocument),
you don't need the WithEvents-thing for that purpose. But note
that you can use WithEvents for other objects such as e.g. the
Application object. This way, you can attach yourself (or your
macros, respectively) to events fired by e.g. Word itself. For the
latter, these events include e.g. WindowActivate, WindowSize,
DocumentBeforePrint and many others...

To get to know this method, add a module-level variable to
your code using WithEvents like this:

Public WithEvents wd As Word.Application

.... and watch the object dropdown which will be extended to
include the newly created variable wd. Choose it to see all the
available Events in the 'procedure'-dropdown.

Ah yes: And don't forget to initialize your object variable
somewhere in your code e.g. in the Document_Open-Event-
Handler :)


Have fun,

Martin
 
J

JGM

Thanks Martin,

You confirmed what I thought...

I have used WithEvents before (to catch a double-click for example), but
never with a Document_Open procedure...
Not being a programmer by training, I was afraid I had missed something
important.

Cheers!
 
Z

ZAZ

There is one case for using the AutoOpen macro. Imagine you are loading 5 templates and they all contain the Document_Open() event handler. Sure, they will all run, but in what order? Implementing code through the AutoOpen (it must be in the Normal.dot!) will ensure that your code will run first. Note: if there's an existing AutoOpen, you must plug into it.


----- Martin Seelhofer wrote: -----

Hello NG
Please, can you clarify this...
... or I have been incrdedibly lucky these past 2 years.

From my point of view, the "AutoOpen"-approach is deprecated and
should not be used anymore, since it does not follow the more object-
oriented approach of Document_Open. So I don't think you have been
too lucky in the past ;-)

The reason why the Document_Open-Event is not fired in Bruce' case
I suspect to be a security issue (value 'High' in Tools - Macro -
Security?)...

However, the Document_Open-event-handler IS the right way
to do it. In my opinion, that is ;-)
I have never included a WithEvents procedure in a class module...
What do you mean by "Heavy-handed"?

Well, the WithEvents-stuff is a way to attach event handlers to an
object variable. However, you only need this, if your object does
not already have a corresponding class-module attached. Since
there is one (a class module) for the Document-Object (ThisDocument),
you don't need the WithEvents-thing for that purpose. But note
that you can use WithEvents for other objects such as e.g. the
Application object. This way, you can attach yourself (or your
macros, respectively) to events fired by e.g. Word itself. For the
latter, these events include e.g. WindowActivate, WindowSize,
DocumentBeforePrint and many others...

To get to know this method, add a module-level variable to
your code using WithEvents like this:

Public WithEvents wd As Word.Application

.... and watch the object dropdown which will be extended to
include the newly created variable wd. Choose it to see all the
available Events in the 'procedure'-dropdown.

Ah yes: And don't forget to initialize your object variable
somewhere in your code e.g. in the Document_Open-Event-
Handler :)


Have fun,

Martin
 

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