VBA Code to change the attached template

B

BABs

I've created a custom template that produces a report doc. It takes about 5
minutes for the template to open, populate the fields and save as a doc. If
I re-open the document, it takes 5 minutes again. I've included code to
update all fields and then unlink before saving, but it is still attached to
MY.dotm and takes forever to open. If I open the doc and go into Add ins and
change the template back to normal.dot, then the doc opens quickly. Is there
code that I can include before the doc saves to attached the normal.dot?
Thx.
 
G

Gordon Bentley-Mix

If you add

ActiveDocument.AttachedTemplate = "Normal.dot"

before you save and close the document, this should do the trick.

However, I don't understand why it should take so long to re-open the
document. Unless you have an AutoOpen or AutoExec macro or a macro that runs
on the Document_Open event, the code in the attached template should not run
when the document is opened. (This is assuming that it's code execution
that's causing the delay on opening the document...)

Have you tried opening the template and inserting a break point at the start
of your code (and leaving the template open) to see what's happening when the
document is opened? You might even consider adding a Document_Open or
AutoOpen macro (with something simple like a message box in it) into your
template just so you can insert the break point and step the code if you
can't find another way to intercept the code execution.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
B

BABs

Gordon thx and I'll try this today. I'm using 2007 and because I have the
document open in code, I'm just going to use
.AttachedTemplate = "normal.dotm"

I don't have any code on the template open. I believe that the template is
so slow opening because I have very many field codes that are linked to an
excel workbook. The fields that I use several times, I've SET to names and
REF them, but there are a lot of fields that I only need once that are just
straight links.
What I'm actually doing is all from access. I have a form where the
employee can pick a job number from the db to generate a report. Access then
runs a query to see how many sites are included in the job, runs a chain of
queries, exports the data to excel and generates a site paragraph doc from a
word template for each site. It then runs a chain of queries for the job
information, exports to excel, generates a report doc from a word template
for the job and inserts the text from the site docs into the report.
There is probably a much better/efficient way to do this, but since I'm new
with this stuff, this was the way I stumbled upon. I ran a report yesterday,
first time it actually worked without getting snagged in the code, for a job
with three sites and it took 34 MINUTES to finish the report!!!
I realize that this is a long time, but I was just happy that it worked.
I'd be open to any suggestions to make it more effecient and thanks again for
the help.
 

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