Changing the Names of Multiple Styles

Written by Allen Wyatt (last updated August 28, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


3

Normand has documents with up to 25-30 custom styles. The names of these styles start with the letters NB. He would like to rename the styles by changing the NB to NF. Normand knows how to do it one by one, but is looking for a way to batch process the changes.

This type of change is easy to do with a macro. Word makes available, to VBA, the name of each style which can also be change through the macro. Here's an example:

Sub ChangeStyleNames()
    Dim s As Style

    For Each s In ActiveDocument.Styles
        If Left(s, 2) = "NB" Then s = "NF" & Mid(s, 3)
    Next s
End Sub

The macro steps through each style in the document and, if the first two letters are "NB", changes those letters to "NF". Note that the macro only needs to work with individual style objects (denoted by the s variable), and that the NameLocal property doesn't need to be explicitly cited. This is because in the absence of an explicit property, the NameLocal property is the one automatically assumed by VBA.

The macro is very fast to run. In testing on a document with almost 400 styles, it took less than 2 seconds to complete. It checks all styles, both built-in and user-defined.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10120) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using List Box Controls

List boxes can be a great tool for getting input from users of your worksheets. This tip describes what list boxes are ...

Discover More

Changing from Absolute to Relative Hyperlinks

It is easy to amass a large number of hyperlinks in a document. You may want to process these hyperlinks in some way, ...

Discover More

Incorrect Suggested Correction

As you type your document, Word flags words and phrases it thinks may be incorrect. The source of these various flaggings ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Changing the Style Gallery

The style gallery appears on the Home tab of the ribbon. You may want to modify how Word displays styles within the ...

Discover More

Printing a List of Custom Styles

You can add any number of styles to your document in order to define how you want your text to appear. If you later want ...

Discover More

Numbering on New Paragraph Doesn't Work as Expected

Numbering in Word can be a bit tricky to navigate. Sometimes it works as it should, and other times it seems to be doing ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2023-08-27 08:50:34

Maria

Hi - I think this was just what I was looking for - but I am a bit of a newbie regarding using the correct syntax for macros and I wonder if anyone can help me customise the macro for my specific needs.

I have sets of styles which I use on various company documents. The styles will be H1Builders, H2Builders, AltH1Builders, AltH2Builders, B1Builders, B2Builders, AppendixBuilders, PartiesBuilders, RecitalBuilders, ScheduleBuilders, SubScheduleBuilders, etc (where 'Builders' represents the company name). Some styles will have 6 or 7 levels.

I want to change just the company name for each level. I do not know how to change the code in the macro to ignore everything in a style name apart from 'Builders' and just to change 'Builders' to 'Lawyers' for every style.

For Each s In ActiveDocument.Styles
If s = "Builders" Then s = "Lawyers"

This seems too simple? I am wary of running this and ending up with a hundreds of styles all called Lawyers+1, Lawyers+2, etc!

Does anyone have any advice for me please?


2022-06-19 16:38:49

Stephane

Hi Allen,
your code is a piece of the puzzle I would like to complete, but as a beginner in coding I don't find a way to make it work for my needs.
I would like to change the last word of the styles in my docs and replace it by another one. My different tries only allowed me to put the new word in front of the name's style and the old word remains…
Any help would be much appreciated! Best, Stephane


2021-10-27 16:50:17

Doug Bond

I ran this code on my document and it only output built-in styles no custom styles:

Sub RenameLocalStyle()
'
' RenameLocalStyle Macro
'
'
Dim sty As Style
For Each sty In ActiveDocument.Styles
'If sty.BuiltIn = False Then
'sty.NameLocal = "A new style name"
Debug.Print sty
'If sty.NameLocal = "Document_Title" Then
' MsgBox sty.NameLocal
'End If
Next sty
End Sub


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.