Finding Unused Styles

Written by Allen Wyatt (last updated June 12, 2020)
This tip applies to Word 97, 2000, 2002, and 2003


5

The capability to create and manage styles is one of the strong features of Word. Styles allow you to quickly and easily apply consistent formatting throughout your document, and update that formatting as your needs change.

Word provides quite a few built-in (predefined) styles, and you can add more as your needs dictate. At some time you might want to determine which styles are not in use in a document. This list could then be used to determine which styles you could easily delete, simply because they are no longer needed.

There is no intrinsic way to create an unused style list in Word. Instead, you need to create a macro to do the job for you. You might think that creating such a macro would be a simple task of looking at which styles Word believes are in use, and then comparing those to the styles which are defined. The problem with this approach is that VBA's InUse property (which applies to Style objects) is used for several purposes. The official definition for the InUse property is that it's True if either of the following two conditions are met:

  • The style is a built-in style that has been modified or applied in the document.
  • The style is a user-defined style that has been created in the document.

What that means is that the InUse property doesn't indicate if a style is actually in use in the document. You could do something to the definition of a style without actually applying it, and that style would be flagged as 'in use' even though there isn't any text in the document actually using the style.

However, it is possible to generate a list of styles not in use by using both the InBuilt and InUse properties in a macro. The following VBA macro uses this approach:

Sub CreateStyleList()
    Dim docThis As Document
    Dim styItem As Style
    Dim sBuiltIn(499) As String
    Dim iStyBICount As Integer
    Dim sUserDef(499) As String
    Dim iStyUDCount As Integer
    Dim sInUse(499) As String
    Dim iStyIUCount As Integer
    Dim iParCount As Integer
    Dim J As Integer, K As Integer
    Dim sParStyle As String
    Dim bInUse As Boolean
    
    ' Ref the active document
    Set docThis = ActiveDocument
    
    ' Collect all styles being used
    iStyIUCount = 0
    iParCount = docThis.Paragraphs.Count
    iParOut = 0
    For J = 1 To iParCount
        sParStyle = docThis.Paragraphs(J).Style
        For K = 1 To iStyIUCount
            If sParStyle = sInUse(K) Then Exit For
        Next K
        If K = iStyIUCount + 1 Then
            iStyIUCount = K
            sInUse(iStyIUCount) = sParStyle
        End If
    Next J
    
    iStyBICount = 0
    iStyUDCount = 0
    ' Check out styles that are "in use"
    For Each styItem In docThis.Styles
        'see if in those being used
        bInUse = False
        For J = 1 To iStyIUCount
            If styItem.NameLocal = sInUse(J) Then bInUse = True
        Next J
        'Add to those not in use
        If Not bInUse Then
            If styItem.BuiltIn Then
                iStyBICount = iStyBICount + 1
                sBuiltIn(iStyBICount) = styItem.NameLocal
            Else
                iStyUDCount = iStyUDCount + 1
                sUserDef(iStyUDCount) = styItem.NameLocal
            End If
        End If
    Next styItem
    
    'Now create the output document
    Documents.Add
    
    Selection.TypeText "Styles In Use"
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
        Selection.TypeText sInUse(J)
        Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph
    
    Selection.TypeText "Built-in Styles Not In Use"
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
        Selection.TypeText sBuiltIn(J)
        Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph
    
    Selection.TypeText "User-defined Styles Not In Use"
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
        Selection.TypeText sUserDef(J)
        Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph
End Sub

The macro first examines every paragraph in the document to determine the names of the styles actually being used in the document. This information is stored in the sInUse array. Then, the macro starts looking through the list which Word thinks are in use--these are the styles that belong to the Styles collection. If the style is not in the sInUse array, then it is added either to the sBuiltIn array (for built-in styles) or the sUserDef array (for user-defined styles). When the comparisons are done, a new document is created that lists the results.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1488) applies to Microsoft Word 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Non-breaking Em Dashes

Need an em dash to be "sticky" on both ends of the dash? Word doesn't provide such formatting, but there are a few ...

Discover More

Formatting Text Files with VBA

Got a bunch of text that you've imported from a text file? Need to make it look better? You can take a stab at it with ...

Discover More

Detecting Hidden Rows

Excel allows you to easily hide rows in a worksheet, so their contents are not visible. Figuring out how to detect where ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Protecting Styles

If you spend a lot of time getting your document styles set "just right," you don't want to take the chance that they ...

Discover More

Printing a Style Sheet

Styles are a fantastic way to format your documents easily and consistently. At some point you may want to print out a ...

Discover More

Ensuring that Spell Checking is Enabled in All Styles

Ever want to enable spell checking in all of the styles within a document, but don't want to check each and every one ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2022-04-29 15:05:57

Aleixa

Hi, Allen!!

Missed defining the variable iParOut


Aleixa


2018-04-15 14:32:46

BCVolkert

I think there may be a couple of typos in your code:

Selection.TypeText "Built-in Styles Not In Use"
Selection.TypeParagraph
For J = 1 To iStyBICount ' <-------------------------=== was "iStyIUCount"
Selection.TypeText sBuiltIn(J)
Selection.TypeParagraph
Next J
Selection.TypeParagraph
Selection.TypeParagraph

Selection.TypeText "User-defined Styles Not In Use"
Selection.TypeParagraph
For J = 1 To iStyUDCount ' <-------------------------=== was "iStyIUCount"
Selection.TypeText sUserDef(J)
Selection.TypeParagraph
Next J
Selection.TypeParagraph
Selection.TypeParagraph


2018-03-09 02:24:39

Ken Endacott

Doug

You are correct, the macro does not detect style usage in headers and footers or text boxes.

For a complete macro see the comments in the tip:
https://word.tips.net/T001337_Removing_Unused_Styles.html


2018-03-08 06:59:54

Doug Bowlds

This subroutine does not seem to work for custom styles that are used exclusively in the header and/or footers. Correct?
It also missed a custom character style that is based on another custom style.


2016-04-01 12:47:18

Michael Virostko

Hi Allen,

I like this VBA code but it will not search headers and footers. I also have multiple sections.

Is there any change to the code for this?

Thanks.

Mike


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.