Link to home
Start Free TrialLog in
Avatar of Gene Jones
Gene Jones

asked on

In MS Word, rename styles in a batch

Hello,

I have a set of styles and I'd like to add a suffix at the end. For example, rename Paragraph to Paragraph-GJJ. I'd like to do it to all the styles I have in my document. How can I do this all at once?
Avatar of DrTribos
DrTribos
Flag of Australia image

You can use VBA but how will you identify the styles that you want to target?
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try this to change not built-in styles
Sub macro()
Dim docActive As Document
Dim stylItem As Style

Set docActive = ActiveDocument

For Each stylItem In docActive.Styles
    If stylItem.InUse = True Then
        With docActive.Range.Find
            .ClearFormatting
            .Text = ""
            .Style = stylItem
            .Execute Format:=True
            If .Found = True And stylItem.BuiltIn = False Then
                stylItem.NameLocal = stylItem.NameLocal & "-GJJ"
            End If
        End With
    End If
Next stylItem

End Sub

Open in new window

Regards
Avatar of Gene Jones

ASKER

Thank you DrTribos & Rgonzo1971 for the response. The code didn't work. It didn't capture all the styles and for some of them, the suffix appeared twice, e.g. -GJJ-GJJ". Also, to clarify, "built in" means if it's a style from Normal.dot? I want to capture all the styles in the document. Even if it's a "built in" style, it's been modified for our purposes. So I want to label it to distinguish it from any other styles that may be coming into the document.

I suppose I can lock the document to prevent any new styles coming in, but my team is used to seeing those labels.
then try
Sub macro()
Dim docActive As Document
Dim stylItem As Style

Set docActive = ActiveDocument

For Each stylItem In docActive.Styles
        If stylItem.BuiltIn = False And Not stylItem.NameLocal like "*-GJJ"Then
             stylItem.NameLocal = stylItem.NameLocal & "-GJJ"
        End If
Next stylItem

End Sub

Open in new window

Thank you, Rgonzo1971. Unfortunately, it did not capture all the styles. Though, the duplication is not there anymore. Screenshot attached.

G
Oops, one more time. Screenshot attached.
screenshot.jpg
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, this one worked!! Thank you so much!!!
The last code added the suffix to all the styles, as I wanted. Thank you!