How to change .attachedtemplate from VBA in Word 2007

G

G. Warner

I have a template that I created in Word 2003. It has a number of macros.
One, which allows the user to save a copy of the active document assigns
Normal.Dot as the ActiveDocument.AttachedTemplate before saving, so that the
macros from the working template are not saved in the copy. The problem now
is that when I run this macro in Word 2007, the
ActiveDocument.AttachedTemplate takes place immediately and breaks the macro
code that was assigning the template and saving the document. Is there a way
to keep the ActiveDocument.AttachedTemplate action from happening
immediately, the way it works in Word 2003?
 
D

Doug Robbins - Word MVP

Show us the code that you are/were using.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

G. Warner

Here is the gist of the code: (line #s added for ease of reference)

1 Set Dlg = Dialogs(wdDialogFileSaveAs) ' create a File Save As
dialog
2 With ActiveDocument
3 bSaved = .Saved ' remember state of doc before
4 If Dlg.Display = -1 Then ' user exited the dialog
via Save/OK button
5 .AttachedTemplate = NORMAL11_DOT ' swap templates
6 Dlg.Execute ' do the SaveAs per
options in dialog

In Word 2007 execution of this Macro terminates after line 5 (line 6 is
never reached) since the .AttachedTemplate is immediate and the Template
containing this macro VBA code is no longer active. In Word 2003 execution
continues and line 6 is executed.

I hope this is clear.
 
G

Graham Mayor

The essence of your code is

Set Dlg = Dialogs(wdDialogFileSaveAs)
With ActiveDocument
.AttachedTemplate = "D:\Word Templates\Normal_11.dot"
Dlg.Execute
End With

The name of the attached template is immaterial as long as it exists. This
code will not work in either Word 2007 OR Word 2003.
The better way to deal with this would be to use your Word 2003 template as
an add-in for Word 2007 then the code will always be available and there is
no need to change the attached template.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

G. Warner

The document, while under control of the template which contains the Macro
code, does execute this code (in essence) in Word 2003. Perhaps I did not
include enough of the code to demonstrate the logic.

Basically I have a document, which is based upon Template1. If the user
wants a local copy of the document, I replace the AttachedTemplate from
Template1 to the Normal.dot for their version of Word, and then save off the
document.

The reason I am changing the AttachedTemplate is because we don't want a
local copy saved with all the Macros from the Template. These macros
intercept the normal Save, SaveAs and other functions. Some of the functions
(macros) from the Template maintain the Word doc in our Document Management
Database Software.

I appreciate the responses - and hope to come to a solution that works in
Word 2007 soon.
 
G

Graham Mayor

I am not sure I understand your logic. The macros stored in a template are
not copied to documents created from the template. They remain in the
template from which the document was created. The macros remain available to
the document while the template is available. If the template is not
available then neither are the macros. When the template is not available,
Word effectively substitutes the user's normal template for the attached
template..

My guess is that this code runs on your Word 2003 system because you have
the macros in your Word 2003 normal template and not the document template.
Word 2007 does not use the same normal template and thus has no access to
the macros there. By changing the document template you lose any macros in
that document template thus the macro stops running, because it no longer
exists. As I said in my previous reply, this is not peculiar to Word 2007,
but to where the macros are stored.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

G. Warner

The code works in Word 2003, however it is NOT in the Normal template - so I
am still confused by the behavior. If anyone has any insight on this, I
would be most appreciative.
 
D

Doug Robbins - Word MVP

If File>New is being used to create the documents, there is NO need to
replace the template as the documents will not contain the macros that are
in the template. If such a document is opened on the computer on which the
template resides, a reference to the template is created so that the macros
in the template are available for use on the document if required.

In Word 2007, if the document is saved in .docx format, it will not/cannot
contain macros in any event.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
G

G. Warner

The document is not being created via File>New. There is a client front end
program which retrieves the Template (containing the macros) from a Document
Management system and creates the new document from that. It then opens the
document (and template) in Word 2007. One of the options that is available
now is to Save a Local Copy. When the user does this, the macro that is
associated with this action prompts the user for the location and name of the
local copy. Once that information is provided, the macro continues execution
and one step is to replace the current .AttachedTemplate (which contains all
the macro code) with a Normal one (either Normal.dot or Normal11.dot). At
the point it executes this step, the Normal template is attached and the
template that was originally used to create the document (which also contains
the running macro) is unloaded and execution stops. This is the point of my
problem. In Word 2003, the template that was originally used to create the
document (which also contains the running macro) is not unloaded, but remains
as a Project Template, but the Normal template becomes the .AttachedTemplate
for the ActiveDocument.

Thank you all for your patience with my descriptions. This has been
frustrating for me. Any help from the forum would be appreciated.

Regards,
G
 
D

Doug Robbins - Word MVP

You will probably need to move the code to an add-in i.e. a template that is
stored in the Word startup folder so that it is loaded globally and thus
available to all documents.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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