View Single Post
 
Old 08-04-2022, 07:58 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This is my approach. I think your strA variables might also be followed by a capital letter (eg Part A, Section B) so I included that possibility.
This approach is to firstly tag all target numbers with by enclosing in findable 'unique brackets'. Then running a separate replace to highlight them, then remove the unique brackets. Because a wildcard search is case sensitive, you need to build the array with both initial cap and lowercase variants.
Code:
Sub HighlightNumbers()
  Dim StrFndA As String, StrFndB As String, i As Long, Rng As Range
  Dim arrA() As String, arrB() As String
  
  StrFndA = "Clause,Paragraph,Part,Schedule" 'highlight numbers after these words 'use initial caps
  StrFndB = "Minute,Hour,Day,Week,Month,Year,Working,Business" 'highlight numbers before these words  'use initial caps
  arrA = Split(StrFndA & "," & LCase(StrFndA), ",")   'both initial cap and lowercase versions of words
  arrB = Split(StrFndB & "," & LCase(StrFndB), ",")   'both initial cap and lowercase versions of words
  
  Application.ScreenUpdating = False
  For Each Rng In ActiveDocument.StoryRanges
    With Rng
      Select Case .StoryType
        Case wdMainTextStory, wdFootnotesStory
          With Rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
            For i = 0 To UBound(arrA)
              .Text = "(" & arrA(i) & ") ([A-Z0-9]{1,})"
              .Replacement.Text = "\1 <<\2>>"
              .Execute Replace:=wdReplaceAll
              .Text = "(" & arrA(i) & "s) ([A-Z0-9]{1,})"
              .Execute Replace:=wdReplaceAll
            Next
            
            For i = 0 To UBound(arrB)
              .Text = "([0-9]{1,}) (" & arrB(i) & ")"
              .Replacement.Text = "<<\1>> \2"
              .Execute Replace:=wdReplaceAll
            Next
            
            Options.DefaultHighlightColorIndex = wdTurquoise
            .Replacement.Highlight = True
            .MatchWildcards = True
            .Text = "\<\<[A-Z0-9]{1,}\>\>"
            .Replacement.Text = ""
            .Execute Replace:=wdReplaceAll
            
            .Replacement.ClearFormatting
            .MatchWildcards = False
            .Text = "<<"
            .Execute Replace:=wdReplaceAll
            .Text = ">>"
            .Execute Replace:=wdReplaceAll
            
            Options.DefaultHighlightColorIndex = wdNoHighlight
            ActiveWindow.View.ShowFieldCodes = True 'Don't highlight cross refs already in fields
              .ClearFormatting
              .MatchWildcards = False
              .Text = "^d"
              .Execute Replace:=wdReplaceAll
            ActiveWindow.View.ShowFieldCodes = False
          End With
        Case Else
      End Select
    End With
  Next Rng
  Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote