G
Greg Maxey
Styles are not my strong point as I rarely do anything with actual Word
documents anymore. That is going to change for a project I am working on.
I have a collection of 30 odd templates that each share some common
custom/built-in styles. What I need to do is have a master template that
contains all of the various style definitions. So that if a style needs to
change I can change it once in the master template and then somehow push it
out to all the various (slave templates) when one of them are used.
I thought that I could load the master template as a Word template Add-In
from the startup folder. Then using AutoNew and AutoOpen macros in the
various templates I could copy the styles from the master template into the
new or opened documents.
Something like this is what I envision:
Sub AutoNewt()
Dim oTmp As Template
Dim oDoc As Word.Document
Dim arrStyles() As String
Dim i As Long
arrStyles() = Split("Heading 1|Heading 2|Heading 3|Heading 4|Body
Text|Testing Style", "|")
For Each oTmp In Templates
If oTmp.Name = "Master Styles.dotm" Then
Exit For
End If
Next oTmp
Set oDoc = ThisDocument
For i = 0 To UBound(arrStyles)
StyleCopyMacro oTmp, oDoc, arrStyles(i)
Next
End Sub
Sub StyleCopyMacro(ByRef oSrcTmp As Template, ByRef oDestDoc As Document,
ByRef pStr As String)
Application.OrganizerCopy Source:=oSrcTmp.FullName,
Destination:=oDestDoc.FullName, Name:=pStr, Object:=wdOrganizerObjectStyles
End Sub
This seems to be working OK based on some very limited testing, but I have
some concerns and questions.
1. All styles are copied each time a new document is created or opened
regardless if the style in the master template has changed or not. This
seems to be overkill, but I don't see any way to compare the styles and copy
only the changed styles.
2. Only designated styles are copied (i.e., the specific styles listed in
the array). I tried using:
For each oStyle in oDoc.Styles
StyleCopyMacro oTmp, oDoc, oStyle.NameLocal
Next oSyle
but this results in command failed errors with some of the style names.
It seems that the only styles that can be copied with the organizer are
those specfic styles that are listed in the organizer (not all sytles in a
document)
3. While the style in the ActiveDocument is copied to match the style in
the Master Template, I don't see a way with code to ensure that the attached
template style is updated. This may be mute in few of the the overkill
issue mentioned above.
Perhaps I am going about this in the wrong way. Interested in any ideas to
meet the objective stated. Thanks.
documents anymore. That is going to change for a project I am working on.
I have a collection of 30 odd templates that each share some common
custom/built-in styles. What I need to do is have a master template that
contains all of the various style definitions. So that if a style needs to
change I can change it once in the master template and then somehow push it
out to all the various (slave templates) when one of them are used.
I thought that I could load the master template as a Word template Add-In
from the startup folder. Then using AutoNew and AutoOpen macros in the
various templates I could copy the styles from the master template into the
new or opened documents.
Something like this is what I envision:
Sub AutoNewt()
Dim oTmp As Template
Dim oDoc As Word.Document
Dim arrStyles() As String
Dim i As Long
arrStyles() = Split("Heading 1|Heading 2|Heading 3|Heading 4|Body
Text|Testing Style", "|")
For Each oTmp In Templates
If oTmp.Name = "Master Styles.dotm" Then
Exit For
End If
Next oTmp
Set oDoc = ThisDocument
For i = 0 To UBound(arrStyles)
StyleCopyMacro oTmp, oDoc, arrStyles(i)
Next
End Sub
Sub StyleCopyMacro(ByRef oSrcTmp As Template, ByRef oDestDoc As Document,
ByRef pStr As String)
Application.OrganizerCopy Source:=oSrcTmp.FullName,
Destination:=oDestDoc.FullName, Name:=pStr, Object:=wdOrganizerObjectStyles
End Sub
This seems to be working OK based on some very limited testing, but I have
some concerns and questions.
1. All styles are copied each time a new document is created or opened
regardless if the style in the master template has changed or not. This
seems to be overkill, but I don't see any way to compare the styles and copy
only the changed styles.
2. Only designated styles are copied (i.e., the specific styles listed in
the array). I tried using:
For each oStyle in oDoc.Styles
StyleCopyMacro oTmp, oDoc, oStyle.NameLocal
Next oSyle
but this results in command failed errors with some of the style names.
It seems that the only styles that can be copied with the organizer are
those specfic styles that are listed in the organizer (not all sytles in a
document)
3. While the style in the ActiveDocument is copied to match the style in
the Master Template, I don't see a way with code to ensure that the attached
template style is updated. This may be mute in few of the the overkill
issue mentioned above.
Perhaps I am going about this in the wrong way. Interested in any ideas to
meet the objective stated. Thanks.