Consulting

Results 1 to 2 of 2

Thread: Stopping a For Each... Loop

  1. #1

    Stopping a For Each... Loop

    I have a document in which users can insert pictures and then give them a caption number. However, the quantity of these is chosen by the user - there can be 20, 30 and so on. Any quantity depending on the subject matter.

    As the procedure is working with fields this is the line of code to start it off:
    For Each oFld In ActiveDocument.Fields
    It progresses through the code until it reaches:
      Next oFld
    As it does not know how many pictures have been inserted it returns back to the top and starts repeating the procedure going into an indefinite loop.

    I've researched the While...Wend; Do..Loop and so on but the big "gotcha" is that it cannot tell how many items it is looking at and then finally break out and exit the Sub.

    As I said, each of the pictures will have a caption starting at 1 and progressing through for each graphic. This numbering of the caption is based on a SEQ field incrementing by 1 each time.

    The whole macro is listed below and it was provided in an earlier post on this forum. Many, many thanks to Graham Mayor and Greg Maxey for setting out the procedures. They work as designed.

    Sub ReplaceFieldsV6()
    'Graham Mayor - http://www.gmayor.com - Last updated - 25 Sep 2018
    Dim oFld As Field, oNewFld As Field
    Dim oRng As Range, oSeq As Range
        For Each oFld In ActiveDocument.Fields
            If oFld.Type = wdFieldSequence Then
                If InStr(1, oFld.Code, "Figure") > 0 Then
                    oFld.Code.Text = Replace(oFld.Code.Text, oFld.Code.Text, "SEQ Figure \* ARABIC \s \* MERGEFORMAT")
                    oFld.Update
                    Set oRng = oFld.Code
                    oRng.MoveStart wdCharacter, -1
                    oRng.Collapse 1
                    oRng.Text = "-"
                    oRng.Collapse 1
                    Set oNewFld = ActiveDocument.Fields.Add(Range:=oRng, _
                                                            Type:=wdFieldStyleRef, _
                                                            Text:="""GA Numbered Heading 1""" & " \s", _
                                                            PreserveFormatting:=False)
                    oNewFld.Update
                End If
            End If
        Next oFld
    
    lbl_Exit:
        Set oFld = Nothing
        Set oNewFld = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Now I've got to stop the continous loop.

    Could someone show me how this can be done, please?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    I can run your code on a document with one Seq field or 100 seq fields. In each case it processes the fields and exits on its own. What is the problem.

    Best Regards,
    Greg Maxey

    The voice said, Cry. And he said, What shall I cry? All flesh is grass and the goodliness thereof is as the flower of the field.
    `Isaiah 40:6
    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
  •