Tech

How to remove unused styles with VBA in Word


If you use Word for writing infrequently, you probably don’t think about Word styles too often. On the other hand, some users may think of style as a forest of formats. One of the main reasons styles confuse users is because there are so many of them and most of them go unused. The Styles pane does a good job of reducing the number of styles you interact with, but ultimately, you may want to remove all unused styles from a complete 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 remove built-in styles at all, so here’s a workaround 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 older version. For your convenience, you can download demonstration .docm, .doc and .cls files. Word for the web does not support VBA procedures.

Why should you remove unused styles in Word?

Most of us use only a few styles in a typical document, but the sample below contains dozens of built-in styles. Add custom styles and you can see things grow out of hand. There are three good reasons you might want to remove unused document styles:

  • In a large document with lots of custom styles that you don’t use, you may see a bit of a performance hit. It’s not a big deal with today’s systems loaded with RAM, but removing unused models is an option.
  • You should delete unused styles in Word documents that you plan to distribute to multiple people. Doing so will make it harder for the recipient to change the format, which can mess up the entire document.
  • You inherit the maintenance of an older document that needs some cleanup.

Picture A shows the Quick Styles library for the presentation document. Click the Styles group dialog box launcher to see more. These are mainly the built-in styles supported by Normal, the underlying template. Some are custom styles. Most documents will not use most of these styles. But this gives you a closer look at the internal styles — there are many, and most of them are built-in, and cannot be deleted.

Picture A

Lots of styles are available but not used in Word documents.
Lots of styles are available but not used in Word documents.

It doesn’t take long for a document to become flooded with lots of custom styles that aren’t used if you save custom styles to the Normal template. In this case, every new material comes with a lot of style baggage. To quickly distinguish how many styles you have using in a Word document, do the following:

  1. Click the dialog box launcher of the Styles group.
  2. At the bottom of the Styles pane, click Options.
  3. In the dialog box that appears, select In use from the Select style in use drop-down menu (Figure BUT).
  4. Click OK.

Figure BUT

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

SIZE

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

As you can see in SIZE, the Styles pane shows only the styles being used now — only seven! However, only one custom style is used. I wonder how many unused custom styles we can get rid of.

How to enter procedures in Word

You could try removing the styles one by one, but that sounds awful. You can use Replace to remove unused styles, but again, you’ll do so one at a time. Using either method, you must know which style is not used. Word VBA procedure in List A mimic an alternative task. I recommend running this process 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 process as shown in Visualization. You can enter the code manually or import a downloadable .cls file. Additionally, macros are in downloadable .docm, .doc, and .cls files. If you enter the code manually, do not paste from this website. Instead, copy the code into a text editor and then paste it into the ThisDocument module. Doing so will remove any virtual web characters that could cause errors.

Visualization

Import the procedure into the ThisDocument module for the current document.
Import the procedure into the ThisDocument module for the current document.

If you’re using the 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 run the process, click the Developer tab, and then click Macro in the Macros group. In the resulting dialog box, shown in Figure E, select the DelUnusedStyles procedure and click Run. Cycling through the Styles collection deletes all unused styles, except for built-in styles.

Figure E

Run DelUnusedStyles
Run DelUnusedStyles.

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

Remember, you can’t delete 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 shows the same list because these styles are in use. The process doesn’t tell you how many were deleted, if any. In the case of this simple document, the process may not remove 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 as Normal.

This or other procedures close in duty have been around for a long time. I can’t credit it, but it can be easily adjusted to fit your needs.

How VBA Procedures Work in Word

The DelUnusedStyles procedure is easy to understand and maintain. It mimics Word’s Replace feature, specifying the style by name as the search string and leaving the replacement string blank. In fact, you can document much of this process and modify it. However, you will find that the write procedure has a lot of unnecessary code and uses explicit, inefficient selections. DelUnusedStyles is concise and effective.

After declaring the variable s as a word type, the For Each structure cycles through all the types in the Type collection. If the Built-in property is False, which means that the type is not a built-in type, the With block sets the required find properties:

  • .ClearFormatting removes any formatting used in the previous search.
  • .Style is set to the name of the current style (which is the Style variable).
  • .Execute runs the task using no text in the search string.

When the .Found property of the current Style is False, the procedure deletes it. This process repeats for all styles in the current document.

If you find yourself using the process 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.



Source link

news7g

News7g: Update the world's latest breaking news online of the day, breaking news, politics, society today, international mainstream news .Updated news 24/7: Entertainment, Sports...at the World everyday world. Hot news, images, video clips that are updated quickly and reliably

Related Articles

Back to top button