View Single Post
 
Old 11-08-2021, 02:06 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

Try this version. It is more correct to search for a Ref field type rather than whether the field text includes "_Ref" somewhere in it.
Code:
Sub myMacro()
  Dim aRng As Range, rngNumber As Range
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .ClearFormatting
    .Text = "Section?[0-9.]{1,}"
    .MatchWildcards = True
    Do While .Execute  ' Loop until Word can no longer find the search string
      Set rngNumber = aRng.Duplicate
      rngNumber.Start = aRng.Words(2).Start
      If rngNumber.Fields.Count > 0 Then 'there is a field
        If rngNumber.Fields(1).Type = 3 Then rngNumber.Font.ColorIndex = vbBlue   'wdFieldRef=3
      Else
        rngNumber.Font.Color = vbRed
        ActiveDocument.Comments.Add Range:=rngNumber, Text:="Missing cross-reference field code"
      End If
      aRng.Collapse Direction:=0      'wdCollapseEnd
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote