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:
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 Office 365.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
The style gallery appears on the Home tab of the ribbon. You may want to modify how Word displays styles within the ...
Discover MoreOne of the features of Word that can cause some problems is one that allows styles to be automatically updated based upon ...
Discover MoreWord is very flexible in what it allows you to search for. One thing it can't do, however, is allow you to search for ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments