Microsoft Software

How to delete unused styles using VBA in Word

If you utilize Word to write the occasional letter, you in all probability don’t take into consideration Word styles too usually. On the opposite hand, some customers would possibly consider styles as a formatting jungle. One of the primary causes styles confound customers is as a result of there are such a lot of of them and most go unused. The Styles pane does job of decreasing the variety of styles you work together with however in the top, you may want to delete all unused styles from a completed doc. In this text, I’ll present you a VBA process that may take away all unused styles in the present doc. Keep in thoughts you could’t delete built-in styles in any respect although, so this can be a answer for paperwork with unused customized styles.

SEE: Software Installation Policy (TechRepublic Premium)

I’m using Microsoft 365 on a Windows 10 64-bit system, however you should utilize an earlier model. For your comfort, you possibly can obtain the demonstration .docm, .doc and .cls information. Word for the net doesn’t help VBA procedures.

Why must you take away unused styles in Word?

Most of us use only some styles in an peculiar doc, however the underlying template accommodates dozens of built-in styles. Add personalized styles, and you’ll see issues develop out of hand. There are three good causes you may want to take away a doc’s unused styles:

  • In a big doc with a number of customized styles that you just’re not using, you would possibly see a little bit of a efficiency hit. It’s not as massive a problem with immediately’s methods loaded with RAM, however eradicating the unused styles is an choice.
  • It’s a good suggestion to eliminated unused styles in a Word doc that you just plan to distribute to lots of people. Doing so will make it tougher for recipients to change the formatting, which could mess up all the doc.
  • You inherit the upkeep of an older doc that wants some cleanup work.

Figure A exhibits the Quick Styles gallery for the demonstration doc. Click the Styles group’s dialog launcher to see much more. These are principally built-in styles supported by Normal, the underlying template. A number of are customized styles. Most paperwork is not going to use most of those styles. But this provides you a peek inside styles—there are numerous them, and most are built-in, which might’t be eliminated.

Figure A

Lots of styles are available but unused in Word documents.
Lots of styles can be found however unused in Word paperwork.

It doesn’t take lengthy for a doc to grow to be overrun with a number of customized however unused styles if you happen to save customized styles to the Normal template. In this case, each new doc comes together with numerous type baggage. To rapidly discern what number of styles you could have in use in a Word doc, do the next:

  1. Click the Styles group’s dialog launcher.
  2. At the underside of the Styles pane, click on Options.
  3. In the ensuing dialog, select 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 solely the styles in use in a Word doc.

Figure C

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

As you possibly can see in Figure C, the Styles pane exhibits solely the styles in use now—solely seven! However, there’s just one customized type in use. I’m wondering what number of unused customized styles we will take away.

How to enter the process in Word

You might attempt deleting styles one after the other, however that sounds terrible. You can use Replace to delete unused styles, however once more, you’d be doing so one after the other. Using both technique, you have to know which styles aren’t getting used. The Word VBA process in Listing A mimics a substitute process. I like to recommend that you just run this process on a duplicate, simply in case.

Listing A

Sub DelUnusedStyles()
'Delete all unused styles, apart from built-in styles,
'in the present doc.
'You cannot delete built-in styles.
Dim s As Style
For Each s In ActiveDocument.Styles
    'Only execute With if present s is not a built-in type.
    If s.BuiltIn = False Then
        With ActiveDocument.Content.Find
            .Style = s.NameLocal
            .Execute FindTextual content:="", Format:=True
            If .Found = False Then s.Delete
        End With
    End If
End Sub

To enter the process, press Alt + F11 to open the Visual Basic Editor (VBE). In the Project Explorer to the left, choose ThisDoc and enter the process as proven 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 information. If you enter the code manually, don’t paste from this internet web page. Instead, copy the code right into a textual content editor after which paste that code into the ThisDoc module. Doing so will take away any phantom internet characters which may in any other case trigger errors.

Figure D

Enter the procedure in the ThisDocument module for the current document.
Enter the process in the ThisDoc module for the present doc.

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

To run the process, click on the Developer tab after which click on Macros in the Macros group. In the ensuing dialog, proven in Figure E, choose the DelUnusedStyles process and click on Run. The process cycles by way of the Styles assortment deleting all unused styles, apart from the built-in styles.

Figure E

Run DelUnusedStyles
Run DelUnusedStyles.

Most doubtless, you gained’t need to work by way of all these steps to run this macro. I like to recommend that you just add it to the Quick Access Toolbar or a customized group. If you need assistance with that, learn How to add Office macros to the QAT toolbar for fast entry.

Remember, you possibly can’t delete the built-in styles, however you possibly can scale back the variety of customized styles obtainable in the Styles pane by altering Recommended to In Use, as we did earlier. In this case, the Styles pane nonetheless shows the identical record as a result of these styles are in use. The process doesn’t inform you what number of have been deleted if any. In the case of this straightforward doc, the process might not have deleted a single type. You’ll need to save this process for these lengthy and outdated paperwork that a number of individuals have labored on earlier than you. Or for all paperwork, if you happen to save all customized styles to Normal.

This process or others shut in process have been round for a very long time. I can’t take credit score for it, nevertheless it’s simply adaptable to fit your wants.

How the VBA process works in Word

The DelUnusedStyles process is straightforward to perceive and preserve. It mimics Word’s Replace function, specifying a method by identify because the discover string and leaving the substitute string clean. In reality, you would document a lot of this process and revamp it. You will discover, although, that the document process has numerous pointless code and makes use of express picks, which is inefficient. DelUnusedStyles is succinct and environment friendly.

After declaring the s variable as a Word type, the For Each assemble cycles by way of all of the styles in the Styles assortment. If the Built-in property is False, that means the type isn’t a built-in type, the With block units the required discover properties:

  • .ClearFormatting removes any formatting used in a earlier discover process.
  • .Style is ready to the present type’s identify (s being the Styles variable).
  • .Execute runs the duty using no textual content in the discover string.

When the present Style’s .Found property is False, the process deletes it. The process repeats this cycle for each type in the present doc.

If you end up using the process usually, add it to Personal.xlsb or to the Quick Access Toolbar (QAT) for fast entry. To be taught extra about Personal.xlsb, learn How to create a VBA process that closes all open workbooks in Excel.


Leave a Reply

Your email address will not be published.Required fields are marked *