Find word at beginning of para and replace style on entire para

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Find word at beginning of para and replace style on entire para

Post by gailb »

I have paragraphs like the below. I would like to find every one of these instances and apply the style "Title"

Chapter Notes — Chapter 1:1-13
Chapter Notes — Chapter 1:14-25
Chapter Notes — Chapter 2:1-26
Chapter Notes — Chapter 2:27-52
Chapter Notes — Chapter 2:53-88

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

Re: Find word at beginning of para and replace style on entire para

Post by HansV »

Like this:

Code: Select all

Sub Macro1()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = "Chapter Notes"
        .Replacement.ClearFormatting
        .Replacement.Text = "^&"
        .Replacement.Style = ActiveDocument.Styles("Title")
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Find word at beginning of para and replace style on entire para

Post by gailb »

Thank you. This is prefect.

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Find word at beginning of para and replace style on entire para

Post by Charles Kenyon »

I was surprised by this. I thought, at first, it worked because the Title style is a paragraph rather than a linked style. However, it is not; it is a linked style.

When using Wildcards, is the ^& to say the entire original paragraph?
Time for me to go back to my Wildcard Cookbook!

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

Re: Find word at beginning of para and replace style on entire para

Post by HansV »

The code doesn't use wildcards, it looks for the literal text "Chapter Notes".
^& is the symbol for the find text. So in this example, "Chapter Notes".
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Find word at beginning of para and replace style on entire para

Post by Charles Kenyon »

Oops, I misread that.

The found text is "Chapter Notes" but the style is applied to the entire paragraph.

With a linked style, this is confusing. If I uncheck my option to disable linked style, select that text, and then apply the Title style, it only applies the character formatting of the Title style to the selected text.
(I hate linked styles but had some hope that they might act consistently.)

This anomaly happens with manual find and replace as well, it is not a vba thing. For this problem, this is a good thing because otherwise I would have to write a bunch more code. (You might not need to, but I would.)

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

Re: Find word at beginning of para and replace style on entire para

Post by HansV »

Microsoft has made it very difficult to keep track of what styles actually do...
Best wishes,
Hans