• About
  • Disclaimer
  • Privacy Policy
  • Contact Us
Thursday, September 21, 2023
Tech Fashion Web
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
Tech Fashion web
No Result
View All Result
How to delete unused styles using VBA in Word

How to delete unused styles using VBA in Word

by Tech Fashion
May 6, 2022
in Technology
0
325
SHARES
2.5k
VIEWS
Share on FacebookShare on Twitter


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

SEE: Software Installation Policy (Tech Republic Premium)

More about software

I use 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 does not support VBA procedures.

Why 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 see things get out of hand. There are three good reasons why you may want to remove unused styles from a document:

  • In a large document with a lot of custom styles that you don’t use, you might see a bit of a performance hit. It’s not much of a problem with today’s systems loaded with RAM, but removing the unused styles is an option.
  • It is a good idea to remove unused styles from a Word document that you want to distribute to many people. Doing so makes it harder for recipients to change the formatting, which can mess up the entire document.
  • You inherit the maintenance of an older document that needs some tidying up.

Image A displays the Quick Styles gallery for the demonstration document. Click the Styles group’s dialog launcher to see even more. These are usually 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 glimpse into styles – there are many, and most of them are built-in, that can’t be removed.

Image A

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

It won’t take long for a document to be flooded 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 see how many styles you have in use in a Word document, do the following:

  1. Click the Starter dialog box of the Styles group.
  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 the styles used in a Word document.
Show only the styles used in a Word document.

Figure C

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

As you can see in Figure C, the Styles panel shows only the styles that are currently in use – only seven! However, only one custom style is in use. I wonder how many unused custom styles we can delete.

How to enter the procedure in Word

You could try removing styles one by one, but that sounds awful. You can use Replace to remove unused styles, but again, you do this one at a time. With both methods, you need to know which styles are not used. The Word VBA procedure in Advertisement A mimics a replacement task. I recommend that you do this procedure on a copy, just in case.

Advertisement 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. In addition, the macro is contained in the downloadable .docm, .doc, and .cls files. If you enter the code manually, do not paste it from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing 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 are using a ribbon version, make sure to save the workbook as a macro 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 cycles through the Styles collection and removes all unused styles except the built-in styles.

Figure E

Run DelUnusedStyles
Run DelUnusedStyles.

Most likely, you don’t want to go through all 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 Add Office macros to the QAT toolbar for quick access†

Keep in mind that you can’t remove the built-in styles, but you can reduce the number of custom styles available in the Styles panel by changing Recommended to In Use, as we did before. In this case, the Styles panel still displays the same list because those styles are in use. The procedure does not tell you how many, if any, have been removed. In the case of this simple document, the procedure may not have removed any style. You 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 in Normal.

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

How the VBA procedure works in Word

The DelUnusedStyles procedure is easy to understand and maintain. It mimics Word’s Replace function, specifying a style by name as the search string and leaving the replace string empty. In fact, you could record and renew much of this procedure. However, you will find that the registration procedure contains a lot of unnecessary code and uses explicit selections, which is inefficient. DelUnusedStyles is concise and efficient.

After the variable s is declared 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 is not a built-in style, the With block sets the necessary search properties:

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

If 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 find yourself using the procedure often, add it to Personal.xlsb or to the Quick Access Toolbar (QAT) for quick access. For more information about Personal.xlsb, read: Create a VBA procedure that closes all open workbooks in Excel†



Source link

Share130Tweet81Share33
Previous Post

10 ways to check ports in Linux to help troubleshoot systems

Next Post

Inside Elon Musk’s Big Plans for Twitter

Tech Fashion

Tech Fashion

Related Posts

Which tool is best for your business?
Technology

Which tool is best for your business?

by Tech Fashion
March 24, 2023
LG Will Spend $5.5 Billion on a Battery Factory in Arizona
Technology

LG Will Spend $5.5 Billion on a Battery Factory in Arizona

by Tech Fashion
March 24, 2023
Intel Announces New vPro Platform Running on 13th Generation Core
Technology

Intel Announces New vPro Platform Running on 13th Generation Core

by Tech Fashion
March 24, 2023
Beat the roaming charges with this eSIM
Technology

Beat the roaming charges with this eSIM

by Tech Fashion
March 23, 2023
Next Post
Inside Elon Musk’s Big Plans for Twitter

Inside Elon Musk’s Big Plans for Twitter

NLRB Finds Merit in Union Accusations Against Amazon and Starbucks

NLRB Finds Merit in Union Accusations Against Amazon and Starbucks

Leave a Reply Cancel reply

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

No Result
View All Result

Categories

  • Business (2)
  • Celebrity (10)
  • Culture (8)
  • Education (8)
  • Entertainment (3)
  • Fashion (12)
  • Food (7)
  • Footwear (7)
  • Health (6)
  • Lifestyle (14)
  • Music (6)
  • Social Media (2)
  • Software (4)
  • Tech Solution (1)
  • Technology (1,884)
  • Travel (12)
  • Website Design (2)

Recent.

Which tool is best for your business?

Which tool is best for your business?

March 24, 2023
LG Will Spend $5.5 Billion on a Battery Factory in Arizona

LG Will Spend $5.5 Billion on a Battery Factory in Arizona

March 24, 2023
Intel Announces New vPro Platform Running on 13th Generation Core

Intel Announces New vPro Platform Running on 13th Generation Core

March 24, 2023
Tech Fashion Web

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Category

  • Business
  • Celebrity
  • Culture
  • Education
  • Entertainment
  • Fashion
  • Food
  • Footwear
  • Health
  • Lifestyle
  • Music
  • Social Media
  • Software
  • Tech Solution
  • Technology
  • Travel
  • Website Design

Recent Posts

  • Which tool is best for your business? March 24, 2023
  • LG Will Spend $5.5 Billion on a Battery Factory in Arizona March 24, 2023
  • Intel Announces New vPro Platform Running on 13th Generation Core March 24, 2023

Contact Us

    © 2021 techfashionweb.com . All rights reserved.

    No Result
    View All Result
    • Home
    • Fashion
      • Footwear
      • Culture
    • Technology
      • Tech Solution
      • Website Design
      • Cyber Security
      • Software
    • Business
    • Digital Marketing
      • SEO
      • Social Media
    • Travel
    • Entertainment
      • Music
      • Celebrity
    • Health
      • Food
    • lifestyle
      • Home
    • More
      • Gaming
      • Gadgets
      • Education
      • Electronics
      • Gadgets
      • Reviews
      • Law

    © 2021 techfashionweb.com . All rights reserved.