template change

D

DualStar

I created a new template based on an old template (same file name). I
have renamed the command buttons so the code is easier to read (in the
old template they were left at default CommandButton1, etc.)and added
in some other functionality. Now I have a fully functional template for
any future documents based on this template and it works fine. I didn't
realize when you open a document based on a template none of the coding
goes with it. Now when someone opens an old document that they have
saved on their computer none of the command buttons work because (I
think) it references the new template. Is there anyway to fix this? I
have tried to open the old document and change the code for the command
buttons in the referenced template file but that doesn't work. I have
also tried to cut and paste the code into ThisDocument of the old
document for a quick fix but that doesn't work either. Any help would
be greatly appreciated. Thank you in advance.
 
C

Chuck Henrich

You say that you named the new template the same name as the old template.
Did you store the new template in the same folder as the old one - ie did you
overwrite the old with the new? If so then the document should open with the
new template attached. If your new template is in a different folder then it
should have a different name to avoid potential confusion, for both yourself
and Word.

In any case you can use an AutoOpen macro to detect what template is
attached to a particular document, and attach a different one if it matches
criteria, eg:

Sub AutoOpen()

Dim strOldTemplate As String
Dim strNewTemplate As String

strOldTemplate = "YourOldTemplate.dot"
strNewTemplate = "c:\YourNewTemplate.dot"
'include the path in strNewTemplate
'if it is not a global template

If ActiveDocument.AttachedTemplate = _
strOldTemplate Then
ActiveDocument.AttachedTemplate = _
strNewTemplate
End If

End Sub

Store the AutoOpen macro in your Normal.dot. Bear in mind the following
(taken from the VBA help file):

"Just like other macros, auto macros can be stored in the Normal template,
another template, or a document. In order for an auto macro to run, it must
be either in the Normal template, in the active document, or in the template
on which the active document is based. "

For more info on auto macros, check the VBA help file on the subject.
 
D

DualStar

Thank you for your response. Alas, I did copy over the old file. I wish
this problem had occured to me sooner but unfortunately I just assumed
that the code was copied to the new document when based on a template.
Live and learn.
 

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