View Single Post
 
Old 07-24-2021, 06:17 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

To do the replacements only on a selected section of the document, the following minor changes would be needed
Code:
Sub RestrictXRefsSelection()
  Dim aFld As Field, aRngXRef As Range, sWord As String, aRngAnchor As Range, sBkmk As String
  Dim arrCode() As String, aRng As Range
  Set aRng = Selection.Range
  For Each aFld In aRng.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
          aRngAnchor.MoveStart Unit:=wdCharacter, Count:=Len(sWord) + 1
          ActiveDocument.Bookmarks.Add Name:=sBkmk, Range:=aRngAnchor
          aFld.Update
          aFld.Select
          Selection.Range.InsertBefore sWord & " ("
          Selection.Range.InsertAfter ")"
        End If
      End If
    End If
  Next aFld
  aRng.Select
End Sub
You can avoid the need for the macro if you manually bookmark the figure number with a unique name and then create a cross-reference pointing at that bookmark.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote