AutoOpen/templates

H

HelpMe!

Can somebody clarify a couple of recent postings?

I have a range of templates which use the Document_New event to launch a
form which is populated and then fills out a document.

This works brilliantly, but what we'd like to do is have the resulting
document do something automatically whenever its opened, preferably without
the need to store code in the document.
If I put a Document_Open in the template, this event will only fire when I
open the template - not the document that the template is attached to.
Similarly, if I put an AutoOpen in the template, it will work for the
template, but not when you open the document that the template is attached
to.

A couple of recent postings have intimated that these techniques should
work, so am I missing something simple. or just being thick?

Any help greatly appreciated.
 
J

Jonathan West

Create an ordinary module in the template, and within in create a subroutine
called AutoOpen. This routine will run automatically whenever a document
based on the template is opened.
 
H

HelpMe!

Thanks for the prompt reply Jonathon.

I've cleared everything out of startup to make sure there were no conflicts.
I've created a brand new empty template, created a module with a public sub
called AutoOpen which does nothing more than launch a msgBox, created a
document based on this template and reopened it....success!!..it launched my
message box.

However, if i then put this same new module into one of the existing
templates(which contain a few modules already) with the same autoopen sub
within it, documents created on this template stubbornly refuse to fire the
autoopen.
Any ideas why?
 
J

Jonathan West

Four possibilities come to mind.

1. There is already an AutoOpen macro in the other template that does
something else. To check, take a look in the code and see.

2. The documents aren't actually connected to the correct template. To
check, open the document, go to Tools, Templates & Add-Ins, anc check that
the correct file & path is shown for the Attached Teamplate.

3. The document and/or the template is corrupted in some fashion. If you
have been editing the macros a lot, then you might be suffering from a touch
of template bloat which can cause unexpected effects. Go here for details on
how to deal with this. "Combatting Template Bloat"
http://word.mvps.org/FAQs/MacrosVBA/TemplateBloat.htm

4. Your macro security settings aren't permitting the code to run. Go to
Tools, Macro, Security, and set the security level to Medium. This will
allow code that is not signed with a deigital signature to be run, but will
warn you first.

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup
 
H

HelpMe!

Thanks Jonathon,

I think point 3 is the most likely culprit.

I've tried exporting the forms/modules etc from one of the existing
templates.

Next, in the blank template I'd created with the working AutoOpen, I started
importing 1st the modules...it still works, then the forms...it still works,
then class modules...it still works, until finally i'd swapped everything
into the new template and..........................It still works!

So, I think we can put this down to one of Words endearing & lovable
features!

Thanks for your help.
 
C

Charles Kenyon

Sub Document_Open in the template works for me (Word 2003). It runs when an
existing document based on the template is opened.

It has to be in the "ThisDocument" object rather than in a module.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
H

HelpMe!

Thanks Charles,

As you can see from the postings with Jonathon, a corrupt template was my
problem and i've now fixed this and can run both AutoOpen & Document_Open.
But this raises the question, which way is best?
What are the pros & cons of each?
 
J

Jonathan West

HelpMe! said:
Thanks Charles,

As you can see from the postings with Jonathon, a corrupt template was my
problem and i've now fixed this and can run both AutoOpen & Document_Open.
But this raises the question, which way is best?
What are the pros & cons of each?

Both work. Therefore use whichever you find easier.
 
C

Charles Kenyon

You can't copy/move macros in ThisDocument with the Organizer, but I do my
editing in the vb editor anyway. They are also one step harder for users to
find.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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