Problems setting a new template
Hi all,
We've got a bunch of documents that have a template attached with a path that no longer exists. I'm making a small macro which replaces the non-existing path with a new one, but I've run into some problems.
It seems that if the missing template isn't found, the document defaults to Normal.dot (fair enough). But, I want to pull the path of the missing template out of the document. My code is:
VB Code:
Sub FixTemplate()
Set sCurrentTemplate = ActiveDocument.AttachedTemplate
sCurrentTemplate = LCase(sCurrentTemplate)
sOldTemplate = LCase("\\some_server\share\missing_template.dot")
If sCurrentTemplate = sOldTemplate Then
ActiveDocument.AttachedTemplate = "C:\program files\templates\text97.dot"
End If
End Sub
This would work beautifully, except that ActiveDocument.AttachedTemplate is "Normal.dot" (since the otehr one can't be found). However, in the Templates and Add-Ins dialog, the path to the missing template is available.
http://www.sunsetgrill.net/tmp/screenshot.jpg
Since the path is still there, I figure there's gotta be some way to pull it out, but damned if I can find it. Any hints?
Re: Problems setting a new template
Rather than looking ActiveDocument.AttachedTemplate, which in the case where the other template path cannot be resolved will always be the normal.dot, you need to look at all available templates in the instance of the word application.
I would suggest that you delete all the non-applicable references then add your new template.
Note: you cannot delete from the templates collection, but only from the AddIns collection.
VB Code:
Sub remove_templates()
Dim dkTmplt As Template
For Each dkTmplt In Application.Templates
If dkTmplt.Name <> "normal.dot" Then
AddIns(dkTmplt.FullName).Delete
End If
Next dkTmplt
End Sub
Re: Problems setting a new template
Thanks for replying! I thought of this too, but the Templates collection only contains loaded templates, not the ones that aren't resolved.
Re: Problems setting a new template
Ahh! When the template reference cannot be found it is removed from the Templates collection. It is still a member of the AddIns collection and has .Installed=False
So, edit the code above and try looping through the AddIns collection, deleting where not installed might be a solution.