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
Find word at beginning of para and replace style on entire para
-
- 3StarLounger
- Posts: 254
- Joined: 09 May 2020, 14:00
-
- Administrator
- Posts: 78412
- 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
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
Hans
-
- 3StarLounger
- Posts: 254
- Joined: 09 May 2020, 14:00
Re: Find word at beginning of para and replace style on entire para
Thank you. This is prefect.
-
- 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
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!
When using Wildcards, is the ^& to say the entire original paragraph?
Time for me to go back to my Wildcard Cookbook!
Attorney Charles Kenyon
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
-
- Administrator
- Posts: 78412
- 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
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".
^& is the symbol for the find text. So in this example, "Chapter Notes".
Best wishes,
Hans
Hans
-
- 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
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.)
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.)
Attorney Charles Kenyon
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
-
- Administrator
- Posts: 78412
- 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
Microsoft has made it very difficult to keep track of what styles actually do...
Best wishes,
Hans
Hans