• Zine
  • Anonymous
  • Green
  • IT
  • Gadgets
  • Tech
  • Cloud
  • Data
  • Network
  • Security
  • Hosting
    • Sunset Host Co
    • Radio Host Co
  • Media
    • WSCA News
    • Sunset Crypto
    • Sustainable Action Now
    • Life.Style Magazine
    • Sunset Daily
    • Sunset Music News
    • Pro Merch Sports News
    • Explore New Jersey
    • Explore NJ News
    • The Zine Weekly
Facebook Twitter Instagram
Monday, May 16
Facebook Twitter Instagram
The Zine Weekly
  • Zine

    To Drive Technological Progress, We Need Worker Power

    May 16, 2022

    Real-time Analytics News for Week Ending May 14

    May 16, 2022

    Announcing Reliable Dedicated Server Hosting Provider with USA, US, New York, California, Texas based IP – TheServerHost – IT Industry Today

    May 16, 2022

    Our study suggests they boost intelligence in children

    May 16, 2022

    Scientists create renewable biocement made entirely from waste materials

    May 16, 2022
  • Anonymous

    How Netflix Is a Joke Addresses a Film Distribution Flaw

    May 14, 2022

    How to Fight Foreign Hackers With Civil Litigation

    May 14, 2022

    Former hacktivist provides security advice

    May 13, 2022

    Anonymous appears to slam Elon Musk’s ‘left wing bias’ comment with new Twitter post

    May 11, 2022

    FACTBOX-The cyber war between Ukraine and Russia

    May 11, 2022
  • Green

    Scientists create renewable biocement made entirely from waste materials

    May 16, 2022

    Proposed improvements to SiC MOSFET power converter technology overcome existing challenges

    May 14, 2022

    Emerging hydrogen storage technology could increase energy resilience

    May 11, 2022

    Microgrid demo to lend a helping hand in India’s green energy transition

    May 9, 2022

    California prepares for energy shortfalls in hot, dry summer

    May 7, 2022
  • IT

    Roe v. Wade reversal could hinder data privacy rights

    May 16, 2022

    Bumper blast! Apple’s smartphone is available for less than 50,000.

    May 15, 2022

    Aura Review | PCMag

    May 14, 2022

    Policymaker proposes commission to oversee digital platforms

    May 13, 2022

    Plot, Cast, and Everything Else We Know

    May 13, 2022
  • Gadgets

    Our study suggests they boost intelligence in children

    May 16, 2022

    Realme TechLife Watch SZ100 India Launch Date Set for May 18, Teased to Offer Up to 12-Day Battery Life

    May 15, 2022

    These high-tech must-have products have actually gotten cheaper

    May 14, 2022

    Musk-Twitter Deal Expected to Close, but Prepared for All Scenarios, Says CEO Parag Agrawal

    May 14, 2022

    Looking for free broadband in Southern California? Here’s how to find it

    May 13, 2022
  • Tech

    Two Twitter bosses ousted ahead of Elon Musk $44bn takeover

    May 13, 2022

    4D composite printing could improve the wings of drones

    May 11, 2022

    How to delete unused styles using VBA in Word

    May 9, 2022

    Best Gas Credit Cards in May 2022

    May 6, 2022

    Eshoo faces rivals from left, right and center in bid to retain Congress seat | News

    May 4, 2022
  • Cloud

    To Drive Technological Progress, We Need Worker Power

    May 16, 2022

    The age of discontinuity | The Manila Times

    May 15, 2022

    Alibaba Cloud-UCSI to support digital economy

    May 15, 2022

    The Matter of Everything, Twelve Experiments that Changed our World: Rich rewards

    May 14, 2022

    VMware sovereign cloud initiative grows to 14 partners

    May 12, 2022
  • Data

    Real-time Analytics News for Week Ending May 14

    May 16, 2022

    Greatest REIT Wannabes Of All-Time: Part I

    May 15, 2022

    Metaverse: ‘Train your robots in the virtual world’ (CXOTalk interview)

    May 15, 2022

    Evoque Partners with Archer Datacenters to Develop 61 Acre

    May 14, 2022

    A Recipe to Migrate and Scale Monoliths in the Cloud

    May 13, 2022
  • Network

    Broadband Nutrition Labels and Deployment See Renewed Focus in Washington

    May 15, 2022

    BHO Network announces its partnership with ConsenSys

    May 15, 2022

    Twitter’s Parag Agrawal after firing top execs: ‘Expect more change for the better’

    May 14, 2022

    The TRON Grand Hackathon 2022 returns for season 2

    May 13, 2022

    PowerShell Tutorial: PowerShell Scripting Basics

    May 13, 2022
  • Security

    Tricity grapples with a new epidemic: Cyber crime

    May 15, 2022

    The foreign diplomatic contingent – Kaieteur News

    May 15, 2022

    What happened when I tried virtual coworking.

    May 14, 2022

    Contest Rules for Dine with Crusher, Wild Florida’s largest gator!

    May 13, 2022

    Hyderabad Police to tap Interpol to trace Nigerian hacker who stole Rs 12.9 crore- The New Indian Express

    May 13, 2022
  • Hosting
    1. Sunset Host Co
    2. Radio Host Co
    Featured

    Announcing Reliable Dedicated Server Hosting Provider with USA, US, New York, California, Texas based IP – TheServerHost – IT Industry Today

    By Sunset Host CoMay 16, 20220
    Recent

    Announcing Reliable Dedicated Server Hosting Provider with USA, US, New York, California, Texas based IP – TheServerHost – IT Industry Today

    May 16, 2022

    Elon Musk Takes a Stand in Controversial Disney Copyright Case

    May 15, 2022

    Announcing Reliable Dedicated Server Hosting Provider with Dubai based IP – TheServerHost – IT Industry Today

    May 14, 2022
  • Media
    1. WSCA News
    2. Sunset Crypto
    3. Sustainable Action Now
    4. Life.Style Magazine
    5. Sunset Daily
    6. Sunset Music News
    7. Pro Merch Sports News
    8. Explore New Jersey
    9. Explore NJ News
    10. The Zine Weekly
    Featured
    Recent

    To Drive Technological Progress, We Need Worker Power

    May 16, 2022

    Real-time Analytics News for Week Ending May 14

    May 16, 2022

    Announcing Reliable Dedicated Server Hosting Provider with USA, US, New York, California, Texas based IP – TheServerHost – IT Industry Today

    May 16, 2022
The Zine Weekly
You are at:Home » How to delete unused styles using VBA in Word
Tech

How to delete unused styles using VBA in Word

Susan HarkinsBy Susan HarkinsMay 9, 2022No Comments7 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Share
Facebook Twitter LinkedIn Pinterest Email

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)

More about Software

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.
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.

Share this:

  • Twitter
  • Facebook

Related

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleManchin says he’d pass parts of Biden’s agenda. But Democrats may have to write the bill for him.
Next Article Announcing Reliable VPS Server Hosting Provider with Malaysia, Kuala Lumpur, Teluk Intan based IP – TheServerHost
Susan Harkins

Related Posts

Two Twitter bosses ousted ahead of Elon Musk $44bn takeover

May 13, 2022

4D composite printing could improve the wings of drones

May 11, 2022

Best Gas Credit Cards in May 2022

May 6, 2022

Comments are closed.

Categories
  • Anonymous (95)
  • Cloud (151)
  • Data Center (134)
  • Gadgets (173)
  • Green Tech (39)
  • Hosting solutions (96)
  • IT News (96)
  • Network (141)
  • Security (134)
  • Tech (96)
  • Web hosting (58)
  • Zine
  • Anonymous
  • Green
  • IT
  • Gadgets
  • Tech
  • Cloud
  • Data
  • Network
  • Security
  • Hosting
    • Sunset Host Co
    • Radio Host Co
  • Media
    • WSCA News
    • Sunset Crypto
    • Sustainable Action Now
    • Life.Style Magazine
    • Sunset Daily
    • Sunset Music News
    • Pro Merch Sports News
    • Explore New Jersey
    • Explore NJ News
    • The Zine Weekly

Subscribe to Updates

Get the latest tech news from the Zine at the Sunset Host Co. and the Radio Host Co.

To Drive Technological Progress, We Need Worker Power

May 16, 2022

Real-time Analytics News for Week Ending May 14

May 16, 2022

Announcing Reliable Dedicated Server Hosting Provider with USA, US, New York, California, Texas based IP – TheServerHost – IT Industry Today

May 16, 2022

Our study suggests they boost intelligence in children

May 16, 2022

Scientists create renewable biocement made entirely from waste materials

May 16, 2022

Roe v. Wade reversal could hinder data privacy rights

May 16, 2022

Tricity grapples with a new epidemic: Cyber crime

May 15, 2022

Broadband Nutrition Labels and Deployment See Renewed Focus in Washington

May 15, 2022
Copyright © 2022. The Zine Weekly, an SCA Entertainment & Media Company. All Rights Reserved.

Type above and press Enter to search. Press Esc to cancel.