Learn how to delete unused types utilizing VBA in Phrase


If you happen to use Phrase to put in writing the occasional letter, you in all probability don’t take into consideration Phrase types too typically. However, some customers may consider types as a formatting jungle. One of many foremost causes types confound customers is as a result of there are such a lot of of them and most go unused. The Types pane does job of lowering the variety of types you work together with however ultimately, you may need to delete all unused types from a completed doc. On this article, I’ll present you a VBA process that can take away all unused types within the present doc. Remember that you possibly can’t delete built-in types in any respect although, so this can be a resolution for paperwork with unused customized types.

SEE: Software program Set up Coverage (TechRepublic Premium)

I’m utilizing Microsoft 365 on a Home windows 10 64-bit system, however you should use an earlier model. On your comfort, you possibly can obtain the demonstration .docm, .doc and .cls recordsdata. Phrase for the online doesn’t help VBA procedures.

Why do you have to take away unused types in Phrase?

Most of us use only some types in an extraordinary doc, however the underlying template incorporates dozens of built-in types. Add custom-made types, and you may see issues develop out of hand. There are three good causes you may need to take away a doc’s unused types:

  • In a big doc with plenty of customized types that you just’re not utilizing, you may see a little bit of a efficiency hit. It’s not as massive a difficulty with right now’s programs loaded with RAM, however eradicating the unused types is an possibility.
  • It’s a good suggestion to eliminated unused types in a Phrase doc that you just plan to distribute to lots of people. Doing so will make it harder for recipients to vary the formatting, which could mess up the whole doc.
  • You inherit the upkeep of an older doc that wants some cleanup work.

Determine A reveals the Fast Types gallery for the demonstration doc. Click on the Types group’s dialog launcher to see much more. These are principally built-in types supported by Regular, the underlying template. A number of are customized types. Most paperwork is not going to use most of those types. However this offers you a peek inside types—there are numerous them, and most are built-in, which might’t be eliminated.

Determine A

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

It doesn’t take lengthy for a doc to turn out to be overrun with plenty of customized however unused types if you happen to save customized types to the Regular template. On this case, each new doc comes together with numerous model baggage. To shortly discern what number of types you might have in use in a Phrase doc, do the next:

  1. Click on the Types group’s dialog launcher.
  2. On the backside of the Types pane, click on Choices.
  3. Within the ensuing dialog, select In Use from the Choose Types In Use dropdown (Determine B).
  4. Click on OK.

Determine B

Show only the styles in use in a Word document.
Present solely the types in use in a Phrase doc.

Determine C

The Word Styles pane shows fewer styles now.
The Phrase Types pane reveals fewer types now.

As you possibly can see in Determine C, the Types pane reveals solely the types in use now—solely seven! Nevertheless, there’s just one customized model in use. I ponder what number of unused customized types we will take away.

Learn how to enter the process in Phrase

You might attempt deleting types one after the other, however that sounds terrible. You should use Exchange to delete unused types, however once more, you’d be doing so one after the other. Utilizing both methodology, it’s essential to know which types aren’t getting used. The Phrase VBA process in Itemizing A mimics a change job. I like to recommend that you just run this process on a duplicate, simply in case.

Itemizing A

Sub DelUnusedStyles()
'Delete all unused types, aside from built-in types,
'within the present doc.
'You'll be able to't delete built-in types.
Dim s As Fashion
For Every s In ActiveDocument.Types
    'Solely execute With if present s is not a built-in model.
    If s.BuiltIn = False Then
        With ActiveDocument.Content material.Discover
            .ClearFormatting
            .Fashion = s.NameLocal
            .Execute FindText:="", Format:=True
            If .Discovered = False Then s.Delete
        Finish With
    Finish If
Subsequent
Finish Sub

To enter the process, press Alt + F11 to open the Visible Primary Editor (VBE). Within the Challenge Explorer to the left, choose ThisDocument and enter the process as proven in Determine D. You’ll be able to enter the code manually or import the downloadable .cls file. As well as, the macro is within the downloadable .docm, .doc, and .cls recordsdata. If you happen to enter the code manually, don’t paste from this internet web page. As a substitute, copy the code right into a textual content editor after which paste that code into the ThisDocument module. Doing so will take away any phantom internet characters which may in any other case trigger errors.

Determine D

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

In case you are utilizing a ribbon model, you’ll want to save the workbook as a macro-enabled file. If you happen to’re working within the menu model, you possibly can skip this step.

To run the process, click on the Developer tab after which click on Macros within the Macros group. Within the ensuing dialog, proven in Determine E, choose the DelUnusedStyles process and click on Run. The process cycles by way of the Types assortment deleting all unused types, aside from the built-in types.

Determine E

Run DelUnusedStyles
Run DelUnusedStyles.

Almost definitely, 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 Fast Entry Toolbar or a customized group. If you happen to need assistance with that, learn Learn how to add Workplace macros to the QAT toolbar for fast entry.

Bear in mind, you possibly can’t delete the built-in types, however you possibly can scale back the variety of customized types out there within the Types pane by altering Really helpful to In Use, as we did earlier. On this case, the Types pane nonetheless shows the identical record as a result of these types are in use. The process doesn’t let you know what number of have been deleted if any. Within the case of this easy doc, the process could not have deleted a single model. 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 types to Regular.

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

How the VBA process works in Phrase

The DelUnusedStyles process is straightforward to know and preserve. It mimics Phrase’s Exchange function, specifying a method by title because the discover string and leaving the change string clean. In truth, you may document a lot of this process and revamp it. You can see, although, that the document process has numerous pointless code and makes use of express choices, which is inefficient. DelUnusedStyles is succinct and environment friendly.

After declaring the s variable as a Phrase model, the For Every assemble cycles by way of all of the types within the Types assortment. If the Constructed-in property is False, that means the model isn’t a built-in model, the With block units the required discover properties:

  • .ClearFormatting removes any formatting utilized in a earlier discover job.
  • .Fashion is ready to the present model’s title (s being the Types variable).
  • .Execute runs the duty utilizing no textual content within the discover string.

When the present Fashion’s .Discovered property is False, the process deletes it. The process repeats this cycle for each model within the present doc.

If you end up utilizing the process typically, add it to Private.xlsb or to the Fast Entry Toolbar (QAT) for fast entry. To be taught extra about Private.xlsb, learn Learn how to create a VBA process that closes all open workbooks in Excel.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*