View Single Post
 
Old 10-21-2021, 08:53 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Documents comprise several separate story ranges which make up the whole - much like a stack of transparencies.

The code I posted earlier acts only on the main story range i.e. the body of the document. Where other ranges need to be processed you meed to address those ranges separately.
Code:
Sub Macro1()
Dim oFld As Field
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
            If oFld.Type = wdFieldSequence Then
                If InStr(1, oFld.Code, "Appendix") > 0 Then
                    oFld.Result.InsertAfter ")"
                    oFld.Result.InsertBefore "("
                End If
            End If
        Next oFld
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For Each oFld In oStory.Fields
                    If oFld.Type = wdFieldSequence Then
                        If InStr(1, oFld.Code, "Appendix") > 0 Then
                            oFld.Result.InsertAfter ")"
                            oFld.Result.InsertBefore "("
                        End If
                    End If
                Next oFldoStory.Fields.Update
            Wend
        End If
        DoEvents
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Set oFld = Nothing
    Exit Sub
End Sub
As your Word crashed - reboot the laptop and see https://www.gmayor.com/what_to_do_when_word_crashes.htm regarding orphaned files.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote