How to delete 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 confound users is because there are so many of them and most go unused. The Styles pane does a good job of reducing the number of styles you interact with but in the end, you might want to delete all unused styles from a finished document. In this article, I’ll 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 though, so this is a solution for documents with unused custom styles.

SEE: Software Installation Policy (TechRepublic Premium)

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

Why should you remove unused styles in Word?

Most of us use only a few styles in an ordinary document, but the underlying template contains dozens of built-in styles. Add customized styles, and you can see things grow out of hand. There are three good reasons you might want to remove a document’s unused styles:

  • In a large document with lots of custom styles that you’re not using, you might see a bit of a performance hit. It’s not as big an issue with today’s systems loaded with RAM, but removing the unused styles is an option.
  • It’s a good idea to removed unused styles in a Word document that you plan to distribute to a lot of people. Doing so will make it more difficult for recipients to change the formatting, which might mess up the entire document.
  • You inherit the maintenance of an older document that needs some cleanup work.

Figure A shows the Quick Styles gallery for the demonstration document. Click the Styles group’s 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 this gives you a peek inside styles—there are a lot of them, and most are built-in, which can’t be removed.

Figure A

Lots of styles are available but unused in Word documents.

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

  1. Click the Styles group’s dialog launcher.
  2. At the bottom of the Styles pane, click Options.
  3. In the resulting dialog, choose In Use from the Select Styles In Use dropdown (Figure B).
  4. Click OK.

Figure B

Show only the styles in use in a Word document.
Show only the styles in use in a Word document.

Figure C

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

As you can see in Figure C, the Styles pane shows only the styles in use now—only seven! However, there’s only one custom style in use. I wonder how many unused custom styles we can remove.

How to enter the procedure in Word

You could try deleting styles one by one, but that sounds awful. You can use Replace to delete unused styles, but again, you’d be doing so one by one. Using either method, you must know which styles aren’t being used. The Word VBA procedure in Listing A mimics a replace task. I recommend that you run this procedure on a copy, just in case.

Listing 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 to 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. In addition, the macro is in the downloadable .docm, .doc, and .cls files. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing so will remove any phantom 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 are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you’re working in the menu version, you can skip this step.

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

Figure E

Run DelUnusedStyles
Run DelUnusedStyles.

Most likely, you won’t want to work through all those steps to run this macro. I recommend that you add it to the Quick Access Toolbar or a custom group. If you need help with that, read How to add Office macros to the QAT toolbar for quick access.

Remember, you can’t delete the 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 doesn’t 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’ll want to save this procedure for those long and old documents that several people have worked on before you. Or for all documents, if you save all custom styles to Normal.

This procedure or others close in task have been around for a long time. I can’t take credit for it, but it’s easily adaptable to suit your needs.

How the VBA procedure works in Word

The DelUnusedStyles procedure is easy to understand and maintain. It mimics Word’s Replace feature, specifying a style by name as the find string and leaving the replace string blank. In fact, you could record much of this procedure and revamp it. You will find, though, that the record procedure has a lot of unnecessary code and uses explicit selections, which is inefficient. DelUnusedStyles is succinct and efficient.

After declaring the s variable as a Word style, the For Each construct cycles through all the styles in the Styles collection. If the Built-in property is False, meaning the style isn’t a built-in style, the With block sets the necessary find properties:

  • .ClearFormatting removes any formatting used in a previous find task.
  • .Style is set to the current style’s name (s being the Styles variable).
  • .Execute runs the task using no text in the find string.

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

If you find yourself using the procedure often, add it to Personal.xlsb or to 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.

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! TechNewsBoy.com is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.