View Single Post
 
Old 10-22-2021, 01:41 AM
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

There is a complexity in using ranges as adding the brackets can often end up inside the field itself. For this reason you need to use the Selection object which is kind of a pain. Try this code version
Code:
Sub AppendixParens()
'   Charles Kenyon and Andrew Lockton using code from Graham Mayor
'   October 21, 2021
'   https://www.msofficeforums.com/word/47835-putting-parentheses-around-sequence-number-caption-label.html
    Dim oStory As Range, oFld As Field, iCount As Integer, aRng As Range
    On Error Resume Next
      For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
          If oFld.Type = wdFieldSequence Then
            If oFld.Code Like "*Table*" Then
              Set aRng = oFld.Result
              oFld.Select       'need to use selection object to make insertbefore go outside of field
              Selection.Range.InsertBefore "("
              Selection.Range.InsertAfter ")"
              iCount = iCount + 1
            End If
          End If
        Next oFld
      Next
    Set oFld = Nothing
    Set oStory = Nothing
    On Error GoTo 0
    MsgBox iCount & " instances bracketed.", vbOKOnly, "Macro complete"
End Sub
Note that I think these brackets are going to play badly with cross-references to the captions.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote