Consulting

Results 1 to 3 of 3

Thread: Finding and replacing a SEQ field

  1. #1

    Finding and replacing a SEQ field

    I'm getting into an awful muddle in trying to find a series of SEQ fields formatted with a particular style.

        Dim strSearch As String
        Dim strReplace As String
        Dim strSEQtype As String
        Dim objRange As Range, objRangeNext As Range, objRange3 As Range
    
        ActiveWindow.View.ShowFieldCodes = True
    
        strSEQtype = "Table"
    
        ' restart sequence after each instance of custom style
        Set objRange = ActiveDocument.Range
        With objRange.Find
            .Format = True
            .Style = ActiveDocument.Styles("Caption Tables")
            While .Execute
                Set objRangeNext = objRange.Duplicate
                objRangeNext.Collapse wdCollapseEnd
                objRangeNext.End = ActiveDocument.Range.End
                With objRangeNext.Find
                    .Format = True
                    .Style = ActiveDocument.Styles("Caption Tables")
                    If .Execute Then
                        objRange.End = objRangeNext.Start - 1
                    Else
                        objRange.End = ActiveDocument.Range.End
                    End If
    
                    ' At this point, objRange covers the range between two
                    ' instances of strCustStyle, or the range following
                    ' the last instance. Now look for the first SEQ field
                    ' within that range.
                    Set objRange3 = objRange.Duplicate
                    With objRange3.Find
                        .Text = "Seq " & strSEQtype & " \* ARABIC"
                        If .Execute Then
                            If objRange3.InRange(objRange) Then
                                objRange3.Text = "Seq " & strSEQtype & " \* ARABIC \r 1"
                            End If
                        End If
                    End With
                End With
                objRange.Collapse wdCollapseEnd
            Wend
        End With
    As you can see, the field I want to find has the string "SEQ Table \* ARABIC" and I want to replace it with "SEQ Table \* ARABIC \r 1"

    When I run the procedure it goes into circular mode and I have to force it to stop.

    Could anyone suggest please, how I could simplify the code so that it does replace the field?

    Thanks for any help.

    Roderick

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You don't actually need a macro for this. Simply press Alt-F9 to expose field codes in the document then use Find/Replace with:
    Find = SEQ Table \* ARABIC
    Replace = SEQ Table \* ARABIC \r 1
    Then press Alt-F9 to hide field codes in the document, followed by Ctrl-A, F9 to update them.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If I understand correctly, you want to reset your Seq Tables field after each instance of Caption Tables style:

    Sub ScratchMfacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/21/2018
    Dim strSEQtype As String
    Dim oRng As Range
    Dim oRngSegment As Range
      ActiveWindow.View.ShowFieldCodes = True
      strSEQtype = "Tables"
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Format = True
        .Style = ActiveDocument.Styles("Caption Table")
        While .Execute
          oRng.Collapse wdCollapseEnd
          oRng.End = ActiveDocument.Range.End
          Set oRngSegment = oRng.Duplicate
          With oRngSegment.Find
            .Text = "Seq " & strSEQtype & " \* ARABIC"
            If .Execute Then
               If oRngSegment.InRange(oRng) Then
                 oRngSegment.Text = "Seq " & strSEQtype & " \* ARABIC \r 1"
               End If
            End If
          End With
        Wend
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •