Word reports the .BaseStyle of styles inaccurately

The problem

Word styles cascade. Out of the box, a few styles have no parent, but most are based on another style. In the object model, this is controlled through the .BaseStyle property.

One of the rules is that a style can only be based on another style of the same type. So, a Character style can only be based on another Character style. A Table style can only be based on another Table style. And so on.

Interrogating the .BaseStyle of a Style routinely returns nonsense. Open a document and try something like this:

Sub TestBaseStyles()

Dim sty As Word.Style

    For Each sty In ActiveDocument.Styles
        'In a clean document, this produces nonsense
        Debug.Print sty.NameLocal, sty.BaseStyle
    Next sty

End Sub

Unless your Style object has been 'primed' in some way, reading the .BaseStyle property produces unmitigated nonsense:

  • Word 2003 reports that most styles are based on Normal style. Clearly character styles, table styles and list styles can't be based on Normal, since it's a paragraph style.
  • Word 2007 and Word 2010 seem to report a random style as the .BaseStyle. Table styles might tell you they're based on character styles. Character styles might tell you they're based on paragraph styles.

Workaround

The best way I've found to work around this is to read the .Description property before reading the .BaseStyle property. This somehow 'primes' the Style object and thereafter it will report the .BaseStyle accurately.

So something like this works perfectly well:

Sub TestBaseStyles()

Dim sty As Word.Style

    For Each sty In ActiveDocument.Styles
        Debug.Print sty.NameLocal, sty.Description, sty.BaseStyle
    Next sty

End Sub

You don't have to output the .Description in any way. Just reading it and assigning to a variable will do. Something like this:

Sub DoSomethingWithMyStyle()

Dim sty As Word.Style
Dim sDescription as String

    For Each sty In ActiveDocument.Styles
        '"Prime" the style
        sDescription = sty.Description

        'Now we can read the .BaseStyle
        MsgBox sty.BaseStyle
    Next sty

End Sub

There are some other properties of a style that will coerce a style into reporting the correct .BaseStyle. That's why this problem does not appear very often: your code may already have read one of the properties that settle the style down. Reading the .Linked property of a paragraph style, for example, is enough to force Word to report the .BaseStyle accurately.

The advantage of using the .Description property is that it appears to be innocuous and applies to every style type, so it's easy to get into the habit of reading the .Description before reading the .BaseStyle.