code to copy style from one template to active doc template

M

mikewillnot

With Word 2003, I have a customized Comment Text style in my
normal.dot template that I'd like to use in every document I open.
The only way I can see to make that happen is by manually copying the
style, using the organizer, from normal.dot to the active document.
I'd like to automate this. I used Macro Recorder, and get the code
below, but it only works on this particular open document. I tried
adjusting it to copy to whatever the activedocument is, but couldn't
get anything to work. Any suggestions would be greatly appreciated.
THANKS.

Sub aaa__copy_style_to_activedoc()
'
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "Normal"
.XMLSchemaReferences.AutomaticValidation = True
.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
End With
Application.OrganizerCopy Source:= _
"C:\Documents and Settings\williamsmi\Application Data
\Microsoft\Templates\Normal.dot" _
, Destination:= _
"[filepathandname]" _
, Name:="Comment Text", Object:=wdOrganizerObjectStyles
End Sub
 
S

Shauna Kelly

Hi

Here's your existing code, commented to explain what it's actually doing and
what might cause problems, and a replacement macro:


Sub aaa__copy_style_to_activedoc()

With ActiveDocument

'Make sure we don't update styles when we open this document.
'This should not be necessary, although it can't hurt.
.UpdateStylesOnOpen = False

'Attach the Normal template to this document. Probably
'not what you want
.AttachedTemplate = "Normal"

'Set automatic validation on all XMLSchemas.
'Do you have an XMLSchema attached to every document on
'which you run this document? Probably not, so we don't
'want this line.
.XMLSchemaReferences.AutomaticValidation = True

'Tell Word that you can only save the document if
'it validates against any XML schema. Probably not something
'you want to enforce when you just want to copy a style.
'So we don't need this.
.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False

End With

'I've re-arranged this to make it easier to read.
'But....
'You don't necessarily have a "normal.dot" file. If you had a new
'installation, Word only creates the file when it needs to.
'However, Word has a property named NormalTemplate that will work
'whether or not the actual Normal.dot file has been created.
'
'And, this will crash if for some reason your normal template
'has no style named Comment Text

Application.OrganizerCopy _
Source:="C:\Documents and Settings\williamsmi\Application
Data\Microsoft\Templates\Normal.dot", _
Destination:="[filepathandname]", _
Name:="Comment Text", _
Object:=wdOrganizerObjectStyles


End Sub


Sub CopyCommentTextStyleToActiveDocument()

'Avoid an error message if for some reason your
'NormalTemplate does not have a style named
'"Comment Text"
On Error Resume Next

'Copy the Comment Text style from the Normal
'template to the active document
Application.OrganizerCopy _
Source:=NormalTemplate, _
Destination:=ActiveDocument.FullName, _
Name:="Comment Text", _
Object:=wdOrganizerObjectStyles

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word




mikewillnot said:
With Word 2003, I have a customized Comment Text style in my
normal.dot template that I'd like to use in every document I open.
The only way I can see to make that happen is by manually copying the
style, using the organizer, from normal.dot to the active document.
I'd like to automate this. I used Macro Recorder, and get the code
below, but it only works on this particular open document. I tried
adjusting it to copy to whatever the activedocument is, but couldn't
get anything to work. Any suggestions would be greatly appreciated.
THANKS.

Sub aaa__copy_style_to_activedoc()
'
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "Normal"
.XMLSchemaReferences.AutomaticValidation = True
.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
End With
Application.OrganizerCopy Source:= _
"C:\Documents and Settings\williamsmi\Application Data
\Microsoft\Templates\Normal.dot" _
, Destination:= _
"[filepathandname]" _
, Name:="Comment Text", Object:=wdOrganizerObjectStyles
End Sub
 
M

mikewillnot

This is outstanding. Thank you very much, Shauna, for the excellent
advice and feedback, Your code, of course, worked like a charm. The
explanations, however, were even more helpful. Thanks again.
 

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