0

I need to format (hidden) all paragraphs starting with a certain word. I have used find-replace

'----set replacement format
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.font
        .Size = 9
        .Color = wdColorTurquoise colour
        .Hidden = True
    End With

'----"COMPARE" AT START OF LINE
    With Selection.Find
        .text = "^13Compare: *^13" 'TODO: need to apply format to PART-exclude 1st para mark!
        .Replacement.text = "" 'keeps original string, just applies repl format to it
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

To find them, I have to search for "^13String*^13", to avoid the same string in other places.

But if I hide the whole find string, the paragraph mark at the end of the preceding paragraph is lost.

Is there a way to change the format of everything except the first ^13?

1 Answer 1

1

What if you don't use find and replace, but instead loop through all paragraphs in your document, check if first word is "Compare:", and if it is, set the font for that paragraph to hidden?

Dim par As Paragraph
For Each par In ActiveDocument.Paragraphs

    If InStr(1, par.Range.Text, "Compare:") = 1 Then par.Range.Font.Hidden = True
        
Next
3
  • Thank you that is perfect! My macro also replaced paragraphs starting with "Section [0—9]{1,}:", but If InStr(1, par.Rangetext, "Section [0—9]{l,}:") = 1 didn't seem to work?
    – Piecevcake
    Commented Aug 29, 2020 at 16:16
  • That'll be because it's looking for text within quotes - what you've included is the wildcard find search string. It won't find "Section [0-9]{1,}:" because that text doesn't appear.
    – Tanya
    Commented Sep 3, 2020 at 1:54
  • You could try the following which checks that there are at least 3 words (so code doesn't fail on rest of tests) and if first word is Section, second word is a number, and third "word" is the colon followed by a space: Dim para As Paragraph For Each para In ActiveDocument.Paragraphs If para.Range.Words.Count >= 3 Then If para.Range.Words(1) = "Section " And IsNumeric(para.Range.Words(2)) And para.Range.Words(3) = ": " Then [do whatever you want here eg delete text or format it] Next
    – Tanya
    Commented Sep 3, 2020 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .