How to Use VBA to Document Custom Styles in a Microsoft Word Document

794014 How to Use VBA to Document Custom Styles in a Microsoft Word Document

Using Visual Basic for Applications (VBA), you can create a macro in Microsoft Word to insert a list of custom styles used in a document. This can be helpful for managing styles, especially in complex documents with many custom styles.

Why Document Custom Styles

Here are some key reasons for documenting custom styles with VBA:

  • Keep track of custom styles – In long Word documents with multiple authors and revisions, it can be difficult to keep track of which custom styles have been defined. Running a macro to get a quick list can help.
  • Easily see all custom styles – While you can view custom styles in the Styles pane, a macro can dump them all into the document itself for easy reference.
  • Share custom styles – If you need to share custom styles with others, inserting a list into the document makes it simple.
  • Standardize templates – When building templates for your organization, listing all custom styles ensures consistency.
  • Troubleshoot issues – If you suspect style corruption or other issues, comparing the style list to what you expected can uncover problems.

The VBA Macro to Document Custom Styles

The following VBA macro prompts the user to choose to insert a list of built-in styles, custom styles, or all styles. It then inserts a table with the name of each style in the document.

Sub ListStyles()

    ' Define variables
    Dim styleType As Integer
    Dim tableRange As Range
    Dim newTable As Table
    Dim i As Integer
    Dim styleName As String

    ' Prompt user for type of styles
    styleType = MsgBox("List styles?" & vbCrLf & "1. Custom styles" & vbCrLf & "2. Built-in styles" & vbCrLf & "3. All styles", vbYesNoCancel + vbQuestion, "List Styles")

    ' Check response
    If styleType = vbCancel Then
        Exit Sub
    ElseIf styleType = vbNo Then
        styleType = 1  
    End If

    ' Insert new paragraph 
    Selection.TypeParagraph()
    Selection.TypeText Text:="Style List"

    ' Insert table
    Set tableRange = Selection.Range
    Set newTable = ActiveDocument.Tables.Add(tableRange, 1, 1) 
    newTable.Borders.InsideLineStyle = wdLineStyleSingle
    newTable.Borders.OutsideLineStyle = wdLineStyleSingle

    ' Add header row
    newTable.Cell(1, 1).Range.Text = "Style Name"

    ' Loop through styles and add rows
    If styleType = 1 Then
        For Each styleName In ActiveDocument.Styles
            If styleName.BuiltIn = False Then
                newTable.Rows.Add 
                i = i + 1
                newTable.Cell(i + 1, 1).Range.Text = styleName.NameLocal
            End If
        Next styleName
    ElseIf styleType = 2 Then
        For Each styleName In ActiveDocument.Styles
            If styleName.BuiltIn = True Then
                newTable.Rows.Add
                i = i + 1
                newTable.Cell(i + 1, 1).Range.Text = styleName.NameLocal
            End If
        Next styleName
    ElseIf styleType = 3 Then
        For Each styleName In ActiveDocument.Styles
            newTable.Rows.Add
            i = i + 1
            newTable.Cell(i + 1, 1).Range.Text = styleName.NameLocal
        Next styleName
    End If

End Sub

How the Macro Works

Here is an overview of what the macro is doing:

  1. Defines variables to use later
  2. Prompts user with MsgBox to choose style type
  3. Checks response and sets default
  4. Inserts new paragraph for label
  5. Inserts table with one column
  6. Adds header row to table
  7. Loops through styles:
  • Checks if built-in or not based on choice
  • Adds rows for each style
  • Inserts local style name into cell

This provides a simple, automated way to document the custom styles used in your Word documents!

Using the Macro

To use the macro:

  1. Open the Visual Basic Editor (Alt+F11)
  2. Paste the above code into a new module
  3. Return to Word document
  4. Click “Developer” tab and view macros
  5. Run the “ListStyles” macro
  6. Select choice when prompted

The macro will instantly generate a table filled with the names of styles based on your choice!

Customizing the Macro

There are many ways you can customize this macro to better meet your specific needs:

  • Change style type checks to include or exclude other types
  • Sort styles alphabetically first
  • Split table into separate sections by style type
  • Indicate number of paragraphs using each style
  • Compare styles to a separate document or template
  • Export list to a separate document
  • And much more!

The core functionality is there to document styles, but you can mold it to output the exact information you need.

Next Steps

Here are some additional things you can do:

  • Learn more VBA – Check out resources for learning VBA in Word to customize this macro
  • Record styles in use – Enhance macro to show where styles are applied in document
  • Create reports – Build reporting tools to share style usage across documents
  • Standardize templates – Leverage style documentation to better standardize templates
  • Clean up styles – Use lists to identify and remove unused styles

Documenting custom styles using VBA is the first step to wrangling complex Word documents. Use this as a starting point to build more robust style management tools.

The ability to programmatically generate a list of custom styles on demand right inside Word unlocks many possibilities to improve consistency and efficiency in your documents.

About The Author