Für Jede Schleife: Einige Elemente übersprungen, wenn die Schleife durch Outlook-Postfach Elemente löschen

Ich entwickeln wollte, um VBA-code:

  1. Durchläuft alle E-Mail-Elemente im Postfach
  2. Wenn es gibt jede Art von anderen Dingen sagen Sie "Kalender Einladung" überspringt das Element.
  3. Findet heraus, dass die E-Mails mit Anhängen
  4. Wenn die angehängte Datei hat ".xml" - Erweiterung und einem bestimmten Titel, in es, speichert es in einem Verzeichnis, wenn er es nicht hält auf der Suche
  5. Stellt alle E-Mails enthält .xml-Anlagen "Gelöschte Objekte" - Ordner nach Schritt 4, und löscht alle E-Mails in diesem Ordner, indem die Schleife.

Code funktioniert perfekt, AUßER;
Zum Beispiel

  1. Es gibt 8 E-Mail erhalten mit ".xml" Datei, die an jede von Ihnen in Ihr Postfach.
  2. führen Sie den code
  3. sehen Sie nur 4 der 8 items sind erfolgreich bearbeitet, die anderen 4 bleiben in Ihren Positionen.
  4. Wenn Sie den code erneut ausführen, jetzt gäbe es 2 Artikel erfolgreich verarbeitet und die anderen 2 bleiben in Ihrem Postfach.

Problem: Nach ausführen des Codes, es soll-Prozess alle Dateien und löscht Sie alle nicht die Hälfte von Ihnen in jedem Lauf. Ich will es verarbeiten alle Elemente zu einem einzigen Lauf.

BTW, dieser code wird ausgeführt, jedes mal, wenn ich das Outlook öffnen.

Private Sub Application_Startup()
'Initializing Application_Startup forces the macros to be accessible from other offic apps

'Process XML emails

Dim InboxMsg As Object

Dim DeletedItems As Outlook.Folder
Dim MsgAttachment As Outlook.Attachment
Dim ns As Outlook.NameSpace
Dim Inbox As Outlook.Folder

Dim fPathTemp As String
Dim fPathXML_SEM As String
Dim fPathEmail_SEM As String
Dim i As Long
Dim xmlDoc As New MSXML2.DOMDocument60
Dim xmlTitle As MSXML2.IXMLDOMNode
Dim xmlSupNum As MSXML2.IXMLDOMNode

    'Specify the folder where the attachments will be saved
    fPathTemp = "some directory, doesn't matter"
    fPathXML_SEM = "some directory, doesn't matter"
    fPathEmail_SEM = "some directory, doesn't matter"

    'Setup Outlook
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.Folders.Item("mailbox-name").Folders("Inbox")
    Set DeletedItems = ns.Folders.Item("mailbox-name").Folders("Deleted Items")


    'Loop through all Items in Inbox, find the xml attachements and process if they are the matching reponses
    'On Error Resume Next
    For Each InboxMsg In Inbox.Items
        If InboxMsg.Class = olMail Then 'if it is a mail item

            'Check for xml attachement
            For Each MsgAttachment In InboxMsg.Attachments

                If Right(MsgAttachment.DisplayName, 3) = "xml" Then

                    'Load XML and test for the title of the file
                    MsgAttachment.SaveAsFile fPathTemp & MsgAttachment.FileName
                    xmlDoc.Load fPathTemp & MsgAttachment.FileName
                    Set xmlTitle = xmlDoc.SelectSingleNode("//title")
                    Select Case xmlTitle.Text
                        Case "specific title"
                            'Get supplier number
                            Set xmlSupNum = xmlDoc.SelectSingleNode("//supplierNum")
                            'Save the XML to the correct folder
                            MsgAttachment.SaveAsFile fPathXML_SEM & xmlSupNum.Text & "_" & Format(Date, "yyyy-mm-dd") & ".xml"
                            'Save the email to the correct folder
                            InboxMsg.SaveAs fPathEmail_SEM & xmlSupNum.Text & "_" & Format(Date, "yyyy-mm-dd") & ".msg"
                            'Delete the message
                            InboxMsg.Move DeletedItems
                        Case Else

                    End Select
                    'Delete the temp file
                    On Error Resume Next
                    Kill fPathTemp & MsgAttachment.FileName
                    On Error GoTo 0
                    'Unload xmldoc
                    Set xmlDoc = Nothing
                    Set xmlTitle = Nothing
                    Set xmlSupNum = Nothing
                End If
            Next
        End If
    Next

    'Loop through deleted items and delete
    For Each InboxMsg In DeletedItems.Items
        InboxMsg.Delete
    Next

    'Clean-up
    Set InboxMsg = Nothing
    Set DeletedItems = Nothing
    Set MsgAttachment = Nothing
    Set ns = Nothing
    Set Inbox = Nothing
    i = 0

End Sub
Sie können auch die Elemente.Restrict-Methode filtern Sie Ihren Posteingang. Dies würde die Rückkehr eine gefilterte Auflistung Elemente, die nur aus Elementen mit Anlagen. Das beschleunigen würde, deinen code etwas durch die Vermeidung der Elemente, ohne Anhänge.

InformationsquelleAutor buri kuri | 2012-05-23

Schreibe einen Kommentar