Hi Gerry
A couple of things.
Issue 1: I would never put code in normal.dot.
If I'm creating code for myself, then I would want to be able to create
it on one machine and copy it to other machines. On development
machines, I want to be able to recreate the client's environment as much
as possible. So I don't want any of 'my' code in normal.dot, and if I've
created an add-in for myself, I want to be able to load it when I need
it, and unload it when I need to have only what the client will have. I
routinely delete normal.dot, as part of reinstating, as much as
possible, a 'clean' development environment.
If I'm creating code for a client, then there's obviously no use putting
it in my normal.dot.
Lars asked "I would like to know whether it is possible to run a
macro/function automatically when a document is loaded from a template?"
It sounded like he just needed a reminder of how to do it. I assumed
that "the template" referred to a template he was creating, and so he
would need to put his code in that template.
An AutoOpen macro in normal.dot would fire every time you opened any
document. An AutoOpen macro in any other template will fire only when a
document based on that template is opened.
Issue 2: I have had occasions on which Document_Open did not fire on
very large documents (say, 600 or more pages with a couple of hundred
tables and images). The same code worked fine on small documents, but
balked on big ones. When I replaced Document_Open with AutoOpen, I had
no further problems. I suspect it's a timing issue related to the fact
that, when Word first opens a document, it repaginates the document, but
interrupts that repagination if the user starts to do something. We can
see the progress of repagination in the status bar. I suspect (but have
no evidence to support my suspicion) that Document_Open seems to get
tangled up with the repagination.
I've not had a problem with Document_New.
The events (Document_Open, Document_New etc) are the newer, and more
structurally sound, ways to run code when a document opens or is
created. And you need to use them when you want to run code external to
Word (eg in a good old-fashioned VB6 COM add-in, or from the .NET world)
when a document is opened or created. But in VBA, I can't see any
advantage to using the events rather than the Auto macros. And I've run
into the problem of the Document_Open event not firing on big documents.
So I tend to use the old-fashioned Auto macros in VBA.
Issue 3: I have given up trying to determine the order in which things
happen in Word. Every so often I try again, and get entirely mixed
results. Recently I was trying to work out the order of the
Document_Change and Document_New events and the loading of a VSTO
assembly. Just when I thought I'd figured it out, I opened a document
that fired them in a different order. I've just learned to code around
this, without relying on any particular order.
Hope this helps.
Shauna
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
fumei via OfficeKB.com said:
Hi Shauna.
"To run code every time a new document is created from the template,
name
the macro AutoNew."
I am just interested in why you prefer AutoNew, rather than
Document_New, to
execute code when a new document is created from the template. I
understand
that "the" template (I think) you have in mind, is most likely
Normal.dot.
AutoNew (in Normal) means every new document created by Word itself,
unless
there is an AutoNew in the template (not Normal). In which case, the
template AutoNew takes over, and the Normal AutoNew is not executed.
The
same applies for a Document_New in Normal. If a Document_New exists
in the
template (not Normal), then it will fire, not the Normal Document_New.
Which brings me to my other question. Whether in Normal, or another
template,
AutoNew fires before Document_New. They are separate and distinct
procedures.
Yet, is there any real difference? Is there any benefit to using
AutoNew
over Document_New? Other than backward compatibility I mean, in
documents
that do not have Document_New. I can't remember, does Word 2000 have
Document_New, or did that start with 2002?
Thanks.
Gerry
Shauna said:
Hi Lars
To run code every time a new document is created from the template,
name
the macro AutoNew.
To run code every time a document based on the template is opened,
name
the macro AutoOpen.
You need to put the code in the template. To do that, use File > Open
to
open the template itself and put your code there.
You can also use Events, but I find the old-fashioned 'Auto' macros
are
more reliable.
More information at
Running a macro automatically when a document is created, opened or
closed
http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
[quoted text clipped - 6 lines]