G
Gary Hillerson
I have a situation where I want to change the template (from TemplateA
to TemplateB) attached to the active document, and then save and close
the document. When I subsequently reopen it, it should be based on the
new template (TemplateB)
I can do exactly this using Word's menus and all's well.
- open the document, then go to Tools|Templates and Add-ins
- click Attach and select a different template
- Save and Close the document
So I want to put the code in TemplateA that emulates using the menu
system. I did this:
Public Sub ChangeTemplate
With ActiveDocument
MsgBox "Change template from" & .AttachedTemplate
.AttachedTemplate = "C:\MyTemplates\TemplateB.dot"
MsgBox "Changed template to TemplateB.dot"
.Close saveChanges:=wdSaveChanges
End With
End Sub
Then I create mydoc.doc, based on TemplateA.dot and save it. Then I
execute the ChangeTemplate Function. And as soon as I change the
..AttachedTemplate property of the ActiveDocument, things get wacky.
The code that follows that statement (the MsgBox and Close) don't
execute.
If I then tell Word to save and close the document, when I reopen the
doc, Word immediately asks if I want to save it. If I say yes, it
closes the doc. If I say no, it closes the doc. If I cancel, I get an
error 4198.
Whew! I think I'm screwing VBA's state and clearly I'm messing up the
document..
Can anyone help with this function?
thanks in advanc,
gary
to TemplateB) attached to the active document, and then save and close
the document. When I subsequently reopen it, it should be based on the
new template (TemplateB)
I can do exactly this using Word's menus and all's well.
- open the document, then go to Tools|Templates and Add-ins
- click Attach and select a different template
- Save and Close the document
So I want to put the code in TemplateA that emulates using the menu
system. I did this:
Public Sub ChangeTemplate
With ActiveDocument
MsgBox "Change template from" & .AttachedTemplate
.AttachedTemplate = "C:\MyTemplates\TemplateB.dot"
MsgBox "Changed template to TemplateB.dot"
.Close saveChanges:=wdSaveChanges
End With
End Sub
Then I create mydoc.doc, based on TemplateA.dot and save it. Then I
execute the ChangeTemplate Function. And as soon as I change the
..AttachedTemplate property of the ActiveDocument, things get wacky.
The code that follows that statement (the MsgBox and Close) don't
execute.
If I then tell Word to save and close the document, when I reopen the
doc, Word immediately asks if I want to save it. If I say yes, it
closes the doc. If I say no, it closes the doc. If I cancel, I get an
error 4198.
Whew! I think I'm screwing VBA's state and clearly I'm messing up the
document..
Can anyone help with this function?
thanks in advanc,
gary