loop items with delete

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

loop items with delete

Post by sal21 »

I loop a items in a outlook folder with:

Code: Select all

For Each objItm In objItms
        If objItm.Attachments.Count > 0 Then
            For Each objAtt In objItm.Attachments
                If objAtt.Filename = "ZIP.zip" Then
                    TEST_DATA = "ASSMF-" & Format$(objItm.ReceivedTime, "DDMMYYYY-HHNNSS") & ".txt"
                    SQL = "SELECT DATA_CONT FROM DATE_CONT WHERE DATA_CONT='" & TEST_DATA & "'"
                    Set RS = CONN.Execute(SQL)
                    If RS.EOF Then
                        objAtt.SaveAsFile "C:\ASS\ZIP.zip"
                        Application.Wait Now + TimeValue("00:00:02")
                        UN_ZIP TEST_DATA
                        CurrentCol = CurrentCol + 1
                    End If
                End If
            Next objAtt
        End If
        objItm.Delete
    Next objItm
i have a 2 prob....

1) the next loop find all items but not the last... perpahs because during the next for, delete the single item founded???

in othe code to correct this prob i use a step -1 in the TO option but i not have idea to insert this tips in the:

For Each objItm In objItms

2) i see the cdoe loop in the items attachment with a For Each objAtt In objItm.Attachments ...next, but i have only one attachment into the sigle item and not more...
is possible to dlete this next loop toi check if the item contain more one attachment?

note:
This is a cut & paste from suggestion code of Hans :grin:

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop items with delete

Post by HansV »

The loop for attachments doesn't hurt, even if there is only one attachment.
For the outer loop, you could use

Code: Select all

    For i = objItms.Count To 1 Step -1
        Set objItm = objItms.Item(i)
        If objItm.Attachments.Count > 0 Then
            For Each objAtt In objItm.Attachments
                If objAtt.Filename = "ZIP.zip" Then
                    TEST_DATA = "ASSMF-" & Format$(objItm.ReceivedTime, "DDMMYYYY-HHNNSS") & ".txt"
                    Sql = "SELECT DATA_CONT FROM DATE_CONT WHERE DATA_CONT='" & TEST_DATA & "'"
                    Set RS = CONN.Execute(Sql)
                    If RS.EOF Then
                        objAtt.SaveAsFile "C:\ASS\ZIP.zip"
                        Application.Wait Now + TimeValue("00:00:02")
                        UN_ZIP TEST_DATA
                        CurrentCol = CurrentCol + 1
                    End If
                End If
            Next objAtt
        End If
        objItm.Delete
    Next i
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: loop items with delete

Post by sal21 »

HansV wrote:The loop for attachments doesn't hurt, even if there is only one attachment.
For the outer loop, you could use

Code: Select all

    For i = objItms.Count To 1 Step -1
        Set objItm = objItms.Item(i)
        If objItm.Attachments.Count > 0 Then
            For Each objAtt In objItm.Attachments
                If objAtt.Filename = "ZIP.zip" Then
                    TEST_DATA = "ASSMF-" & Format$(objItm.ReceivedTime, "DDMMYYYY-HHNNSS") & ".txt"
                    Sql = "SELECT DATA_CONT FROM DATE_CONT WHERE DATA_CONT='" & TEST_DATA & "'"
                    Set RS = CONN.Execute(Sql)
                    If RS.EOF Then
                        objAtt.SaveAsFile "C:\ASS\ZIP.zip"
                        Application.Wait Now + TimeValue("00:00:02")
                        UN_ZIP TEST_DATA
                        CurrentCol = CurrentCol + 1
                    End If
                End If
            Next objAtt
        End If
        objItm.Delete
    Next i

work fine! Tks as usual :thankyou: