Search for paragraph plural or not

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Search for paragraph plural or not

Post by SmallFry »

I need to search for, example

Paragraph 2.1
paragraph 4.5.6

Looks for paragraph whether capitalized or not and then bolds. All this is fine.

Code: Select all

.Text = "[Pp]aragraph [0-9]{1,}"
And this works, but not when the Paragraphs is plural. So I thought adding an asterisk after Paragraph would help, but it catches more than I want.

Code: Select all

.Text = "[Pp]aragraph* [0-9]{1,}"
If I use the above with the asterisk, it catches
Paragraph A3.1.3.2 and Paragraph A3.2.3.2. This is the total man-hours earned from this attachment for FMF. Now return to the AFMS FMF core application instructions and continue on at Step 3.
Here is the entire procedure

Code: Select all

Sub BoldParagraph()
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Text = "[Pp]aragraph* [0-9]{1,}"
        While .Execute
            Selection.MoveEndWhile Cset:="0123456789."
            If Selection.Characters.Last = "." Then Selection.MoveEnd Unit:=wdCharacter, Count:=-1
            If Selection.Range.Font.Bold = True Then GoTo Skip
            If MsgBox(Prompt:="Bold this reference?", Buttons:=vbYesNo + vbQuestion, Title:="Emphasize References") = vbYes Then
                Selection.Style = ActiveDocument.Styles("Strong")
                Selection.Text = StrConv(Selection.Text, vbProperCase)
            End If
Skip:
            Selection.Collapse wdCollapseEnd
        Wend
    End With
End Sub

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

Re: Search for paragraph plural or not

Post by HansV »

Unfortunately, Word does not support searching for zero or one occurrences of a character. So I'm afraid you'll have to search for

"[Pp]aragraph [0-9]{1,}"

and then for

"[Pp]aragraphs [0-9]{1,}"
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Search for paragraph plural or not

Post by SmallFry »

Got it. Thanks.

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

Re: Search for paragraph plural or not

Post by HansV »

You might use

Code: Select all

.Text = "<[Pp]aragraph*> [0-9]{1,}"
This will find "paragraph 3" and "paragraphs 4.3", but also "paragraphic 99". That will probably not be a problem, but that is for you to judge.
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Search for paragraph plural or not

Post by SmallFry »

Thanks again Hans. I think I might have to stick with your first suggestion as I also have

Paragraph A3.1.3.2

and that is causing more problems.

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

Re: Search for paragraph plural or not

Post by HansV »

Ah yes, * is too "greedy".
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Search for paragraph plural or not

Post by macropod »

HansV wrote:Unfortunately, Word does not support searching for zero or one occurrences of a character. So I'm afraid you'll have to search for

"[Pp]aragraph [0-9]{1,}"

and then for

"[Pp]aragraphs [0-9]{1,}"
You could, of course, search for:
"[Pp]aragraph{1,}[A0-9.]{1,}"
Paul Edstein
[Fmr MS MVP - Word]

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Search for paragraph plural or not

Post by StuartR »

macropod wrote: You could, of course, search for:
"[Pp]aragraph{1,}[A0-9.]{1,}"
That's clever
StuartR


SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Search for paragraph plural or not

Post by SmallFry »

Thanks Paul. This works well also.