Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

vba code to delete all messages in a folder

Hi,

I need vba code to delete all messages in a folder. I currently have another macro that saves all files automaticaly, and would like to deete the files after they have been saved.

Andy
0
bellboy2k
Asked:
bellboy2k
  • 2
  • 2
1 Solution
 
bruintjeCommented:
Hello bellboy2k

you could use something like

----------
Sub CleanupFolder()
Dim objNS As NameSpace
Dim objInbox As MAPIFolder
Dim objMailItem As Object
 
  Set objNS = Application.GetNamespace("MAPI")
  Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
  intItems = objInbox.Items

  'Delete the Item
  For i = intItems To 1 Step -1
    Set objMailItem = objInbox.Items(i)
    objMailItem.Delete
  Next i
 
  Set objNS = Nothing
  Set objInbox = Nothing
  Set objMailItem = Nothing
 
End Sub
----------

of course change the folder to the one you need else it will delete all mail in your inbox folder

hope this helps a bit
bruintje
0
 
bellboy2kAuthor Commented:
bruintje,

I got an error on intItems = objInbox.Items any ideas?

Andy
0
 
bruintjeCommented:
sorry that should be the count

intItems = objInbox.Items.Count
0
 
bellboy2kAuthor Commented:
bruintje,

Thanks thst worked great, how would I get it to delete the files from a subfolder of the Inbox?

Andy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now