Macro to replace one style with another

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Macro to replace one style with another

Post by ChrisGreaves »

Word2003, as usual.
I am not attempting anything clever here.

Code: Select all

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/30/2023 by Chris077
'
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Body Text")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("TutBodyText")
    Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
        .Text = "^p"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Feeling lazy I recorded the macro into my Normal.dot and noted with approval that all the paragraphs styled "Body Text" now had "TutBodyText" applied. The originally recorded macro used null strings for the Find .Text and for the Replacement .Text.
Untitled.png
That this worked during macro recording satisfied me that I had selected/typed the paragraph style names correctly.

I opened a second document, it too needed treatment, so I ran the macro from the Normal.dot - but no paragraph styles were changed.

I have since tried using "any character" and "find what text" for the Find .Text and for the Replacement .Text.
I have since tried using paragraph marks "^p" for the Find .Text and for the Replacement .Text.

Why would a simple macro work during recording but not work during playback?

The file in question is attached.
Thanks in advance
Chris
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro to replace one style with another

Post by HansV »

It works if I comment out the two lines that mention Shadow:

Code: Select all

Sub Macro1()
'
' Macro1 Macro
'
'
    With Selection.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles("Body Text")
        .Replacement.ClearFormatting
        .Replacement.Style = ActiveDocument.Styles("TutBodyText")
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Macro to replace one style with another

Post by ChrisGreaves »

HansV wrote:
30 May 2023, 18:56
It works if I comment out the two lines that mention Shadow:
Thank you Hans.
What suggested "Shadow" to you?
I shall try it with "", "^?" and "^p" over the next few days.
Cheers, Chris
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro to replace one style with another

Post by HansV »

I was simply trying things...
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Macro to replace one style with another

Post by ChrisGreaves »

HansV wrote:
30 May 2023, 19:56
I was simply trying things...
In what version of MSWord? I'm curious about the life-expectancy of this design Feature. :evilgrin:
Cheers, Chris
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro to replace one style with another

Post by HansV »

Word in Office 2021.
Best wishes,
Hans

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: Macro to replace one style with another

Post by snb »

Code: Select all

Sub M_snb()
  For Each it In ActiveDocument.Paragraphs
    If InStr(c00, it.Style) = 0 Then c00 = c00 & vbLf & it.Style
    If it.Style = "Body Text" Then it.Style = "Heading 2"
  Next
  
  MsgBox c00
End Sub