Problems with Document templates and macros

  • Thread starter Daníel B. Sigurgeirsson
  • Start date
D

Daníel B. Sigurgeirsson

Hi everyone,

I have a problem, and I'm hoping someone can help me solve it :) My
scenario looks like this:

1. I have a Word document template that contains several macros. This
document contains (among other things) some combo boxes a command button,
and an event handler is attached to this button. The code for this event
handler is stored in the document template.
2. The end user creates a document based on this document template. After
the document has been created he must select certain values from the combo
boxes and then press the command button to confirm his selection. Now he can
save the result document.
3. For reasons that are beyond this issue, the code that is executed when
the document is initially generated from the document template, must contain
the line

ActiveDocument.AttachedTemplate = ""

but this breaks the behaviour descibed in steps 1 and 2, since it requires
the document template and the code and event handler that is defined there.

What I would like to do is to write some code that during the creation of
the document will copy the relevant event handler and all code it requires
to the end document, and then detach the document template from the
document. So far I haven't had much luck with this. It appears that the
event handler must be stored in the "ThisDocument" section and not in a
separate code module. Is this correct? How can I programmatically add code
to this section?

With best regards,
Daniel
 
P

Peter Hewett

Hi Daniel

Setting:
ActiveDocument.AttachedTemplate = ""
just sets the attached template to Normal.dot. It's impossible for a
document not to have an attached template.

If you absolutely have to why not reset the attached template as the last
thing you do. This could be conditional upon the user having entered the
prerequisite data. You could either write your own macro to intercept Words
FileSave/FileSaveAs commands, or if you want to be really flash you could
implement an event handler and catch the DocumentBeforeClose or
DocumentBeforeSave events.

I'm not sure what you're trying to achieve or think you'll achieve by
setting the new documents attached template to Normal.dot?

HTH + Cheers - Peter
 
D

Daníel B. Sigurgeirsson

Hi Peter,

The reason for why I need to detach the document template is because in
Office 2003, when a document has been generated from the template and is
then saved, Word asks if it should also save the template file. Which is
something I absolutely don't want.

Thanks for your tip, I'll definitely look into it.

Daniel
 
P

Peter Hewett

Hi Daníel

I don't have W2003 installed (I don't have it full stop!) so I can't test out
my recommendations. But in all versions of Word to date it only asks you to
save the template, if somehow something has actually changed the template.
Anyway you if you haven't already, try the following code just before you
save/close your document:

' Tell Word the template has already been saved (so don't do it again)
ActiveDocument.AttachedTemplate.Saved = False
or
' Save the template
ActiveDocument.AttachedTemplate.Save

HTH + Cheers - Peter
 
P

Peter Hewett

Oooops!!!

The line should have read:

' Tell Word the template has already been saved (so don't do it again)
ActiveDocument.AttachedTemplate.Saved = True

Cheers - Peter
 
C

Charles Kenyon

What is happening is your code is altering your template file. Otherwise you
wouldn't be getting this question. If it is creating toolbars, etc. why not
simply build those once into the template and get rid of or disable the
code.

Otherwise, you could include at the end of your code:
ActiveDocument.AttachedTemplate.Saved = True

This is dangerous in that if the user makes other changes that go into the
template (i.e. saving styles to the template) your code-created changes will
be saved as well. Unless you need on-the-fly modifications of your
interface, you are probably best-off creating them once in your template
rather than each time the template is used.

Also, while you can write code that inserts code into documents, what do you
do about macro security? Many locations automatically trust code in properly
stored templates but not in documents. I haven't a clue what the code would
look like to digitally sign the code in a document and it would require that
the _user_ have the permission to sign code. I can't think of a much bigger
hole in security even if users are not told about it.
--

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