• Outlook VBA – Delete Mail Item after Sending

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Outlook VBA – Delete Mail Item after Sending

    Author
    Topic
    #1773510

    I have a VBA macro that is used in Outlook that automatically sends an email. I don’t want that email to be saved in the user’s Sent folder.

    First, I tried setting DeleteAfterSubmit to True. That did not delete the email from the Sent folder.

    Code:
     Set msg = Outlook.Application.CreateItem(olMailItem)
        msg.Subject = sMsg
        msg.Body = sBody
        msg.To = “user@domain.com”
        msg.DeleteAfterSubmit = True
        msg.Send
    

    My second attempt was as follows, but it takes too long because the For loop processes the Items from the oldest to the most recent. There could be hundred or even thousands of Items in the Sent folder. (Debug.Print shows me the subject line of each Item in the For loop.)

    Code:
        For Each oMail In ns.GetDefaultFolder(olFolderSentMail).Items
            Debug.Print oMail.Subject
            If oMail.Subject = sSubject Then
                Beep
                Set NewMail = oMail
                Exit For
            End If
        Next
        If Not NewMail Is Nothing Then
            NewMail.Delete
        End If

    It would make sense to process the Items in reverse order (Newest first), since we know that this Item will be the most recent Sent item. Can someone offer a suggestion on how to do that… or how to ensure the DeleteAfterSubmit will function.

    Many thanks.

    Reply To: Outlook VBA – Delete Mail Item after Sending

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: