none
Delete mail item in an Outlook folder with VBA. RRS feed

  • Question

  • I use the codes below to delete all the mail items in an outlook (2007) folder, it works but left with one not deleted.

    Kindly advise how to delete all mail items in an outlook folder.

    For Each oMailItem In oMyFolder.Items
      If oMailItem.Class = olMail
        oMailItem.Delete
      End If
    Next

    Thank you.


    TS Lim

    Friday, March 2, 2012 3:15 AM

Answers

  • I found the solution to my own  question.

    I run the code two times:

    For Each oMailItem In oMyFolder.Items
      If oMailItem.Class = olMail
        oMailItem.Delete
      End If
    Next

    For Each oMailItem In oMyFolder.Items
      If oMailItem.Class = olMail
        oMailItem.Delete
      End If
    Next

    The second time will delete the last email not deleted by the  frist time.


    TS Lim

    • Marked as answer by TS Lim Friday, March 2, 2012 8:00 AM
    Friday, March 2, 2012 8:00 AM

All replies

  • I found the solution to my own  question.

    I run the code two times:

    For Each oMailItem In oMyFolder.Items
      If oMailItem.Class = olMail
        oMailItem.Delete
      End If
    Next

    For Each oMailItem In oMyFolder.Items
      If oMailItem.Class = olMail
        oMailItem.Delete
      End If
    Next

    The second time will delete the last email not deleted by the  frist time.


    TS Lim

    • Marked as answer by TS Lim Friday, March 2, 2012 8:00 AM
    Friday, March 2, 2012 8:00 AM
  • This is simpler, not very elegant, but simpler:

    While objFolder.Items.Count > 0
        For Each Msg In objFolder.Items
            Msg.Delete
        Next
    Wend

    Wednesday, August 27, 2014 8:55 PM