PDA

View Full Version : Solved: Change Attachedtemplate with VBA



J. Zuidhoek
02-22-2008, 02:22 AM
Hi,

I have some templates on a network share and i want to change the location of te attachted Template. So al the documents based on the old template must be attachted to the new template.

I thought it was simple to place VBA code in the old template and the trigger the Document_Open event and then change the attachtedtemplate like this (this is a example):

Private Sub Document_Open()
If LCase(Application.Dialogs(wdDialogToolsTemplates).Template) = LCase("c:\temp\template.dot") Then
ActiveDocument.AttachedTemplate = "c:\temp2\template.dot"
End If
end sub

This option works great when the template name is not the same. When the template has the same name (in this case template.dot) then the attachtedtemplate is not changed..

I tried serveral different locations but nothing works. It works when the template has a different name, but i don't want that.

Is there a solution for this problem.

Thanks.
Jolanda.

Nelviticus
02-22-2008, 03:18 AM
If you're putting the code in the old template, it will only run if the document is attached to the old template - so you don't need the 'if' statement.

J. Zuidhoek
02-22-2008, 03:36 AM
That's a good tip.. but it don't solve my problem

Nelviticus
02-22-2008, 03:38 AM
OK then, try setting AttachedTemplate to nothing (.AttachedTemplate=""), then setting it to the new template path.

J. Zuidhoek
02-22-2008, 03:44 AM
Thanks for your quick reply, but this is not working

TonyJollans
02-22-2008, 06:11 AM
Templates are a bit funny in this regard.

Try using the Dialog:


With Application.Dialogs(wdDialogToolsTemplates)

.Template = "c:\temp2\template.dot"
.Execute
End With

fumei
02-22-2008, 11:31 AM
You guys are all wet. Soaking.

AttachedTemplate has both a .Name (the default value interpreted by VBA!), and a .Path.

Read that again.

ActiveDocument.AttachedTemplate = "C:\Templates\Letter.dot"

What do you think you get if you ran:
Msgbox ActiveDocument.AttachedTemplate


Do you think you get: "C:\Templates\Letter.dot"

No.....you don't.

You get: "Letter.dot"

You can redirect the AttachTemplate to the same named template in a different folder by setting the .Path.

Say the AttachedTemplate is "C:\Templates\Letter.dot"

Say there is a Letter.dot in: "X:\Yadda\Blah\OtherStuff\Templates"

ActiveDocument.AttachedTemplate.Path = _
"X:\Yadda\Blah\OtherStuff\Templates\"


Do NOT forget to add the "\"!

TonyJollans
02-22-2008, 12:16 PM
The blindingly obvious once again escaped my view.

I'm drying off now :)

fumei
02-22-2008, 12:51 PM
Sorry Tony, but it is MY turn to eat crow.

.Path is read-only.

munch munch munch....mmm, tasty crow....

It is ME that is soaking wet, drenched in fact, submerged to my eyeballs.

It is read-only.

TonyJollans
02-22-2008, 01:19 PM
So the blindingly obvious can bite too!

This is one of those tasks - often required due to server changes - that should become easier with the new xml format, because it will be possible to do without using Word. I don't really know how but it must be possible - I'll have to try to work it out sometime.

senthilkumar
02-22-2008, 09:17 PM
Hi Jolanda,:hi:

you can attach the template by using the below code.

Private Sub Document_Open()
If LCase(Application.Dialogs(wdDialogToolsTemplates).Template) = LCase("c:\temp\template.dot") Then
ActiveDocument.AttachedTemplate= application.NormalTemplate.Path & "Normal.dot"
ActiveDocument.AttachedTemplate = "c:\temp2\template.dot"
End If
end sub

or
Private Sub Document_Open()
If LCase(Application.Dialogs(wdDialogToolsTemplates).Template) = LCase("c:\temp\template.dot") Then
ActiveDocument.AttachedTemplate= ""
ActiveDocument.AttachedTemplate = "c:\temp2\template.dot"
End If
end sub


Thanks,
S. Senthil Kumar.

J. Zuidhoek
02-24-2008, 11:53 PM
Thanks for all your replies... I tried it all, but it's not working. The template path is not changing.. Suggestions are still welcome..

Thanks..
Jolanda.

Nelviticus
02-25-2008, 03:01 AM
Hmm. Does the template in the new location have to keep same name as the old one? It might work if you give it a new name as well as a new location. Unfortunately I can't test it as I now only have access to Office 2007 :(

J. Zuidhoek
02-25-2008, 04:32 AM
Yes, the template must have the same name..

Nelviticus
02-25-2008, 05:01 AM
Well I've just had a play around (using Office 2003 file formats in Office 2007) and if the new template has a different name it will work, but if it has the same name it will fail. I just can't get it to work if the old and new templates have the same name.

So ... time for a work-around. In your new template folder, create a new template called TemplateIntermediate.dot. Make it a hidden file if you like, to stop people from creating documents based on it. Put code in it to attach the document to the new version of Template.dot. In your old template, put code to attach the document to TemplateIntermediate.dot.

The first time the document opens it will be attached to TemplateIntermediate.dot. The second time it opens it will be attached to the new version of Template.dot.

It's more fiddly but at least it will work.

J. Zuidhoek
02-25-2008, 05:27 AM
Thanks.. i think this is the only way it will work. I'm going to implement this solution.

Just curious, does it work in office 2007 (change the attachedtemplate to a different folder and the same name)

TonyJollans
02-25-2008, 07:16 AM
Can you tell me exactly what you are doing - including, specifically:

* The location of the document
* The location of the original template
* The location of the new template
* Whether there is a template with the same name in the folder with the document
* How you are checking where the attached template is before changing
* How you are checking where the attached template is after changing
* Whether the template is changed (ie needs saving) before trying to set the new one
* Whether the template is changed (ie needs saving) after trying to set the new one

Nelviticus
02-25-2008, 07:55 AM
Just curious, does it work in office 2007 (change the attachedtemplate to a different folder and the same name)

No, it doesn't work in Office 2007 either.

I created two folders, one called TemplateOld and one called TemplateNew. In each one I put a template called TemplateTest.dot (Word 2003 format). The one in TemplateNew had an AutoOpen method that displayed a message box saying "this is the new template". The one in TemplateOld had an AutoOpen method that displayed a message showing ActiveDocument.AttachedTemplate.Path and .Name, then set .AttachedTemplate to "", saved the document, then set .AttachedTemplate to the full path and name of the new template.

I created a blank document, attached it to the file in TemplateOld, saved it and closed Word. I then opened the document again.

The code ran fine but didn't change anything - the attached template was still the old path.

When I changed the last step to point to a differently-named template it worked.

There's no need to check where the old template is first since that's where your code is. I don't remember what the .Saved state of the documents was and I've now deleted them.

Regards

TonyJollans
02-25-2008, 08:13 AM
Where did you save the document? This is critical.

If you saved it in folder TemplateOld, which also contained a template called TemplateTest (the name is all that matters) then, no matter what Path might have been in the Document, Word would have found the Template in the same Folder as the Document (before even trying to look at the path) and temporarily, for that edit session, have attached the 'wrong' Template.

If you want to check out what Word actually does you must save the Document in a different folder.

Nelviticus
02-25-2008, 09:11 AM
You're right you know. In my tests the document was in the same folder as the old template. When I moved the document somewhere else, the code worked.

TonyJollans
02-25-2008, 10:36 AM
You're right you know. :)

It's really good of Word to hide completely from you the fact that the code has worked, isn't it?

J. Zuidhoek
02-27-2008, 12:09 AM
Yes you can say that.. thanks for all your replies..

Jolanda.