Master Styles

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.
 
G

Greg Maxey

I have been experimenting and the following looks promising for my purposes:

I put the Master Styles.dotm in the startup folder (maybe I could just point
to it and not have to have it in startup) then run the following macros in
each slave template:

Sub AutoNew()
Dim oTmp As Template
Dim oTmpAttached As Template
Dim oDoc As Word.Document
For Each oTmp In Templates
If oTmp.Name = "Master Styles.dotm" Then
Exit For
End If
Next oTmp
Set oDoc = ActiveDocument
Set oTmpAttached = oDoc.AttachedTemplate
With oDoc
.UpdateStylesOnOpen = True
.AttachedTemplate = oTmp.FullName
.UpdateStylesOnOpen = False
.AttachedTemplate = oTmpAttached
End With
End Sub

Sub AutoOpen()
Dim oTmp As Template
Dim oTmpAttached As Template
Dim oDoc As Word.Document
For Each oTmp In Templates
If oTmp.Name = "Master Styles.dotm" Then
Exit For
End If
Next oTmp
Set oDoc = ActiveDocument
Set oTmpAttached = oDoc.AttachedTemplate
If oDoc.Type = wdTypeDocument Then
With oDoc
.UpdateStylesOnOpen = True
.AttachedTemplate = oTmp.FullName
.UpdateStylesOnOpen = False
.AttachedTemplate = oTmpAttached
End With
End If
End Sub


Thoughts?
 
J

Janine

Hi Greg,

My thoughts,

I would call the macro in AutoNew if these templates are their only
templates to set base styles and font but I wouldn't use AutoOpen as once
the document is created then tweaking a style is part of the course in many
environments - although it is rare as base styles should cover 99.9%.
Therefore I would not use AutoOpen as it would kill the current tweaked
style(s).

Often templates based on one master style template are tweaked individually
as templates so it may not be a good idea to override automatically. That's
all I can see going "wrong".

Otherwise full steam ahead - it will update the attached style template as
you wish. My "location" is below:

Sub Styles_Firm()

Dim StylesLocation As String

On Error GoTo StylesError

StylesLocation = "Y:\SETUP\styles.dotx"

With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = StylesLocation
End With

Exit Sub

StylesError:
MsgBox "Styles template unable to be located - " & StylesLocation, ,
"ribbonspace"

End Sub

Janine
www.ribbonspace.com
 
G

Greg Maxey

Janine,

So when when you attach a master template to the document in order to update
the styles you leave that template attached? In my code I was attaching the
master template, updating the styles, and then going back to the base
template for the document. Does it matter?

Thanks.
 
J

Janine

Greg,

I tend to leave it attached and uncheck update when doing manually. But I
work in many challenging environments!

What I can tell you is:

If I manually update on Citrix Web Server - say I attach a styles.dot to my
letter because I use styles and they do not (except Normal) then I attach
styles.dot, check box Update styles and usually leave it attached but
uncheck the update box. That way I know all styles are available to the new
document because I've attached the template.

When I copy/cut from offline to online Server letter in real time and paste
in active document screen - I will only get the styles I've used offline as
they paste.

Body 2
Heading 5
B1

Note that the template on the server has only 5 styles to begin with). So
any style I don't use is not pasted, any style with same name, eg Heading 1
is updated (keep source formatting) and any new style B1 is added.

So should I wish to edit my letter online (rather than offline) I will not
have all the styles my styles.dot has available to the template unless I
leave it attached and save the active document with the attached template.

Does that make sense? So yes, I leave the proper template attached to have
available ALL styles more often than not.

Quickstyles is the new way to go about this from what I can see. So save
your masterstyle set as a quickstyle set and write the macro to update using
the quickstyle set. I'm still testing this in 2007 but it looks promising.
The colours and new styles are added (from what I can see) and styles with
same names are added.
Perhaps write a macro using the quickstyle set to update (think I've got one
of those actually) attaching the quickstyle.


Janine.
 
G

Greg Maxey

Janine,

<Does that make sense?

Well, my entering argument was that styles are not my strong point ;-)

Say I have a collection of templates (Letterhead, Contract, Memo, Fax, etc).
That all share some common styles (Built-in and firm custom). In addition
to styles these templates contain may contain AutoText entries.

I maintain a master template that contains all of the common style
definitions. I see from your code that I probably don't need it to be in
the startup folder.

When I create a new document (say a letter), I want to make sure all of the
letterhead styles are updated to match the master template styles. It then
seems that I would want to re-attach the letterhead template to access and
or create new template stored items such as AutoText.

Does that make sense?

I haven't experimented any with the Quickstyle sets but I will take a look
at it.

Thanks.
 

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