How to remove unused styles using VBA in Word

If you use Word to write the occasional letter, you probably don’t think about Word styles too often. On the other hand, some users might think of styles as a formatting jungle. One of the main reasons styles confuse users is that there are so many of them and most of them go unused. The Styles pane helps reduce the number of styles you interact with, but ultimately you may want to remove all unused styles from a finished document. In this article, I will show you a VBA procedure that will remove all unused styles in the current document. Keep in mind that you can’t delete built-in styles at all, so this is a workaround for documents with unused custom styles.

TO SEE: Software Installation Policy (TechRepublic Premium)

I’m using Microsoft 365 on a 64-bit Windows 10 system, but you can use an older version. For your convenience, you can download the .docm, .doc and .cls demo files. Word for the web does not support VBA procedures.

Why should you delete unused styles in Word?

Most of us only use a few styles in a regular document, but the underlying template has dozens of built-in styles. Add custom styles and you can watch things spiral out of control. There are three good reasons why you might want to remove unused styles from a document:

  • In a large document with lots of custom styles that you don’t use, you might notice a performance hit. This isn’t as big a problem with today’s RAM-heavy systems, but deleting unused styles is an option.
  • It’s a good idea to remove unused styles from a Word document that you plan to distribute to a large number of people. This will make it harder for recipients to change formatting, which could mess up the whole document.
  • You are inheriting maintenance from an older document that needs cleanup work.

Figure A displays the Quick Styles gallery for the demo document. Click on the Styles group dialog launcher to see even more. These are mostly built-in styles supported by Normal, the underlying template. A few are custom styles. Most documents will not use most of these styles. But it gives you an overview of the styles – there are a lot of them, and most of them are built-in, which can’t be removed.

Figure A

Many styles are available but not used in Word documents.
Many styles are available but not used in Word documents.

It doesn’t take long for a document to be overrun with many custom but unused styles if you save custom styles in the Normal template. In this case, each new document comes with a lot of style baggage. To quickly discern how many styles you have used in a Word document, do the following:

  1. Click the Styles group dialog launcher.
  2. At the bottom of the Styles pane, click Options.
  3. In the resulting dialog box, choose In Use from the Select styles in use drop-down list (Figure B).
  4. Click OK.

Figure B

Show only styles used in a Word document.
Show only styles used in a Word document.

Figure C

The Word Styles pane now displays fewer styles.
The Word Styles pane now displays fewer styles.

As you can see in Figure C, the Styles pane displays only the currently used styles—just seven! However, only one custom style is used. I wonder how many unused custom styles we can delete.

How to enter the procedure in Word

You can try deleting the styles one by one, but that looks awful. You can use Replace to remove unused styles, but again, you would do it one at a time. Using either method, you need to know which styles aren’t being used. The Word VBA procedure in List A mimics a replacement task. I recommend performing this procedure on a copy, just in case.

List A

Sub DelUnusedStyles()
'Delete all unused styles, except for built-in styles,
'in the current document.
'You can't delete built-in styles.
Dim s As Style
For Each s In ActiveDocument.Styles
'Only execute With if current s isn't a built-in style.
If s.BuiltIn = False Then
With ActiveDocument.Content.Find
.ClearFormatting
.Style = s.NameLocal
.Execute FindText:="", Format:=True
If .Found = False Then s.Delete
End With
End If
Next
End Sub

To enter the procedure, press Alt + F11 to open the Visual Basic Editor (VBE). In the project explorer on the left, select ThisDocument and enter the procedure as shown in Figure D. You can enter the code manually or import the downloadable .cls file. Additionally, the macro is found in downloadable .docm, .doc, and .cls files. If you enter the code manually, do not paste it from this webpage. Instead, copy the code into a text editor, and then paste that code into the ThisDocument module. This will remove any ghost web characters that might otherwise cause errors.

Figure D

Enter the procedure in the ThisDocument module for the current document.
Enter the procedure in the ThisDocument module for the current document.

If you’re using a ribbon version, be sure to save the workbook as a macro-enabled file. If you are working in the menu version, you can skip this step.

To perform the procedure, click the Developer tab and then click Macros in the Macros group. In the resulting dialog box, shown in Figure E, select the DelUnusedStyles procedure and click Run. The procedure goes through the Styles collection, removing all unused styles except built-in styles.

Figure E

Run DelUnusedStyles
Run DelUnusedStyles.

Most likely you won’t want to follow all of these steps to run this macro. I recommend adding it to the Quick Access Toolbar or a custom group. If you need help, read How to Add Office Macros to the QAT Toolbar for quick access.

Remember that you can’t remove built-in styles, but you can reduce the number of custom styles available in the Styles pane by changing Recommended to In Use, as we did earlier. In this case, the Styles pane still displays the same list because these styles are in use. The procedure does not tell you how many were deleted, if any. In the case of this simple document, the procedure may not have deleted a single style. You will want to save this procedure for long and old documents that several people have worked on before you. Or for all documents, if you save all custom styles in Normal.

This procedure or other related tasks have been around for a long time. I can’t take credit for it, but it’s easily adaptable to your needs.

How VBA procedure works in Word

The DelUnusedStyles procedure is easy to understand and manage. It mimics Word’s Replace function, specifying a style by name as the search string and leaving the replace string blank. In fact, you could save much of this procedure and rearrange it. You will find, however, that the registration procedure contains a lot of unnecessary code and uses explicit selects, which is inefficient. DelUnusedStyles is succinct and effective.

After declaring the s variable as a Word style, the For Each construct loops through all the styles in the Styles collection. If the Built-in property is False, which means the style is not a built-in style, the With block sets the necessary lookup properties:

  • .ClearFormatting removes any formatting used in a previous search task.
  • .Style is set to the name of the current style (where s is the Styles variable).
  • .Execute executes the task without using any text in the search string.

When the current Style’s .Found property is False, the procedure removes it. The procedure repeats this cycle for each style in the current document.

If you use the procedure often, add it to Personal.xlsb or the Quick Access Toolbar (QAT) for quick access. To learn more about Personal.xlsb, read How to Create a VBA Procedure That Closes All Open Workbooks in Excel.

Comments are closed.