Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Deleting With a For Each Loop

    When deleting items in a DOM collection, it is always better to use a For Each loop:

    Code:
    For Each Rng In ActiveDocument.StoryRanges
      For Each Fld In Rng.Fields
       Fld.Delete   
      Next Fld 
    Next Rng
    Or a backwards countdown:
    Code:
    For currentField = myStory.Fields.Count To 1 Step -1
         myStory.Fields(currentField).Delete 
    Next currentField
    It seems sometimes the For Each approach misses, skips, or gets confused by items in the list.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi ForEachLoop,

    Using a For Each Loop is more efficient than iterating through a collection via a counter (whether forwards or backwards). When using a counter for item deletion, you need to go backwards; otherwise every second instance will be missed. There's nothing about the For Each Loops in your first code snippet that would explain any errant behaviour - works fine for me.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    How about this (with only one uncommented Delete statement each time, and manually deleting all the lines before each run)? Only the last case deletes all the lines.

    Code:
     
    Public Sub TestLines()
    On Error GoTo MyErrorHandler
     
    'Sets up a lot of lines
    Dim i As Long
    For i = 1 To 300
         ActiveDocument.Shapes.AddLine 10, i * 2, 2000, i * 2
    DoEvents
    Next
     
    Dim myShape As Shape
    For Each myShape In ActiveDocument.Shapes
         'myShape.Delete
     
    DoEvents
    Next
     
    Dim j As Long
    For j = 1 To 300
         'ActiveDocument.Shapes(j).Delete
    DoEvents
    Next
     
    Dim k As Long
    For k = ActiveDocument.Shapes.Count To 1 Step -1
         'ActiveDocument.Shapes(k).Delete
    DoEvents
    Next
     
    MsgBox "Shapes remaining: " & ActiveDocument.Shapes.Count
     
    Exit Sub
    MyErrorHandler:
    MsgBox "TestLines" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub
    Last edited by ForEachLoop; 2011-03-15 at 10:16. Reason: Variable incorrect

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi ForEachLoop,

    As you've apparently discovered, a For Each loop behaves differently when deleting items from the shapes collection than it does when deleting items from the fields collection. I'd call it a bug.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for the confirmation!

  6. #6
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Wellington, New Zealand
    Posts
    143
    Thanks
    0
    Thanked 1 Time in 1 Post
    Just out of interest, what version of Word did you use to test this? I use 2003 SP3, and for me the For Each routine works correctly.

    I've seen comments elsewhere (http://social.msdn.microsoft.com/For...5-6d7389cc719d, for example) that suggest that Word 2010 works differently - and, presumably, incorrectly.

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    My favourite solution to this type of problem is
    Code:
    With ActiveDocument
      While .Shapes.Count > 0
        .Shapes(1).Delete
      Wend
    End With
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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