The simplest way, using VBA, to reset part of a style definition (e.g. the font name), so it inherits the definition of the style it is based on
Article contributed by Henk van Boeijen
In Word, styles can be (and usually are) based on other styles. If you create a new style, Word bases it on the style of the paragraph that is currently selected, unless you explicitly change this in the “Modify Style” dialog. The style it is based on is called its “base style” (or sometimes, the “parent style”).
1. |
If it's a paragraph style, the new style will inherit (in an object-oriented fashion) all of its font and paragraph properties from its base style, and will be defined by Word only as “the definition of the base style plus anything you have defined to be different from the base style”. So in the Format + Style dialog, you will see definitions like: “Normal + Indent: Hanging 0.25", Space After 6 point”. In this example, only the Indent and the Space After setting are held in the style definition. So if you change the Font in the Normal style, the font in the child style will automatically change, too. But if you change the “Space After” property of the Normal style, the “Space After” property of the child style will not change, because that property is stored in the child style's definition and so is not inherited. Similarly, if you see a definition like:
“Normal + Font Arial”
in the Format + Style
dialog, then nothing but the font name is stored in the style definition. So
if you change the Font of your Normal style, the font in the child style
will not
change; but if you change the Space After property of the Normal style,
the Space After property of the child style will change. |
2. |
Character styles behave similarly except that only font properties are defined or inherited. So you will see character style definitions in the Format + Style dialog like “Default Paragraph Font + Font color: Dark Blue”. In this example, nothing is stored in the character style definition except the font colour. Everything else is inherited from the underlying paragraph style of whichever paragraph you happen to have applied the character style to. |
So a style can be based on another style which in turn is based on another style, and so on. In this way you can form an hierarchy tree of styles. Each member in that tree inherits the formatting options of its parent and adds its own specific options to it. Used sensibly, this mechanism is absolutely essential to your ability to maintain complex documents and templates.
Another example
Style "Normal":
- Font.Name = "Times New Roman"
- Font.Size = 11
- Font.Bold = False
Style "Special":
- this style is based on "Normal"
- Font.Size = 9
Style "MoreSpecial"
- this style is based on "Special"
- Font.Bold = True
All three styles share the same font name. The font name is defined in “Normal” and is inherited by “Special” and “MoreSpecial”. When the font name in “Normal” is changed to “Arial”, this change also applies to “Special” and “MoreSpecial”.
The font size is only shared by “Special” and “MoreSpecial”: 9 points is defined in “Special”. It overrides the font size setting of “Normal” which is 11 points. “MoreSpecial” inherits 9 points from “Special”.
If the font size of “Normal” is changed to 12 points, this setting will not be inherited by “Special” or “MoreSpecial”.
In some cases you may want to delete formatting properties in a style's definition, in order that it should inherit those properties from its parent (or base) style.
To do this in VBA, use the following logic:
Dim oStyle
As Style
Set oStyle = ActiveDocument.Styles("Special")
oStyle.Font.Name = oStyle.BaseStyle.Font.Name
After running this code, the font name will no longer be stored in the style definition of the “Special” style, but will now be inherited from the Normal style.
If you need to do this more than once, you could make your life easier by calling a subroutine like the following, which clears the font name of a given style. It accepts a style name (of type String) or a WdBuiltinStyle constant, or a style object, as its argument. So you could call it like this:
ClearStyle "List Number"
or like this:
ClearStyle wdStyleListNumber
or like this:
ClearStyle ActiveDocument.Styles("List number")
If the style is not based on another style, the routine exits without generating an error.
Public Sub ClearStyleFont(oStyle
As Variant)
Dim oBaseFont As Font
With ActiveDocument.Styles(oStyle)
If .BaseStyle = ""
Then
'There is no
base style, nothing to clear
Else
Set
oBaseFont = .BaseStyle.Font
With
.Font
.Name =
oBaseFont.Name
'Add other font properties here if needed
End With
Set
oBaseFont = Nothing
End If
End With
End Sub
You could use exactly the same logic to clear paragraph properties from a style definition, so that they are inherited from the base style.
If you wanted to clear all font and paragraph properties from a style definition, (including any list numbering, language and borders definitions, although unfortunately, not including Frames definitions), in order that you can start defining your own properties for the style knowing that you are starting with a clean slate, you could use the following subroutine.
Public Sub ClearStyle(oStyle
As Variant)
With ActiveDocument.Styles(oStyle)
If
.BaseStyle <> "" Then
.Font = .BaseStyle.Font
.ParagraphFormat
= .BaseStyle.ParagraphFormat
End If
End With
End Sub