View Single Post
 
Old 08-21-2021, 12:15 AM
laith93 laith93 is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Mr. Andrew, I edited your code to work for the entire document according to your code
Thank so much again

Code:
Sub RestrictXRefsForEntireDoc()

'Putting cross references number inside parenthesis and convert Figure and Table to lowercase
'Developed by Mr. Andrew - msofficeforums.com

  Dim aFld As Field, aRngXRef As range, sWord As String, aRngAnchor As range, sBkmk As String
  Dim arrCode() As String, aRng As range, aRngBk As range
  For Each aFld In ActiveDocument.Fields
    If aFld.Type = wdFieldRef Then
      Set aRngXRef = aFld.result
      sWord = LCase(Trim(aRngXRef.Words(1)))
      If sWord = "table" Or sWord = "figure" Then
        arrCode = Split(Trim(aFld.Code), " ")
        sBkmk = arrCode(1)
        If ActiveDocument.Bookmarks.Exists(sBkmk) Then
          Set aRngAnchor = ActiveDocument.Bookmarks(sBkmk).range
           If InStr(LCase(aRngAnchor.Text), sWord) > 0 Then
          aRngAnchor.MoveStart Unit:=wdCharacter, Count:=Len(sWord) + 1
          ActiveDocument.Bookmarks.Add Name:=sBkmk, range:=aRngAnchor
          End If
          aFld.Update
          aFld.Select
           If Trim(LCase(aRngXRef.Words.First.Previous.Text)) <> sWord Then
          Selection.range.InsertBefore sWord & " ("
          Selection.range.InsertAfter ")"
        End If
      End If
    End If
    End If
  Next aFld
End Sub
Reply With Quote