deleting outlook items from excel vba

Premanshu

Board Regular
Joined
Oct 2, 2007
Messages
91
Hi,

I have a code which can delete mail items from outlook express from inbox folder if a certain mail has a particular subject line.

My problem is when i try to run this same code for deleting mails from the deleted items folder making the neccessary change in the macro i get an error.

Can anyone please help on this:-

the code is as below



Code:
Sub DeleteMail()
Dim out_app As Outlook.Application
Dim folders As Outlook.Namespace
Dim myfolder As Outlook.MAPIFolder
Dim mails_itm As MailItem


Set start_fm = Range("a1")
Set out_app = New Outlook.Application
Set folders = out_app.GetNamespace("mapi")
Set myfolder = folders.GetDefaultFolder(olFolderInbox)


'''''this below line i'll use when i'll try to delete mail from deleted items
'Set myfolder = folders.GetDefaultFolder(olFolderDeletedItems)



For Each mails_itm In myfolder.Items
If mails_itm.Subject = "target" Then
mails_itm.Delete
End If
Next mails_itm

End Sub
many thanks in advance :)

Regards,
Premanshu
 

Some videos you may like

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Premanshu

Board Regular
Joined
Oct 2, 2007
Messages
91
thank you so much Akashwani.....
this is really a great help.
:)

Regards,
Premanshu.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,134
Office Version
2013
Platform
Windows
Premanshu
Try this code
Code:
Sub RemoveAllItemsAndFoldersInDeletedItems()
    Dim oDeletedItems As Outlook.Folder
    Dim oFolders As Outlook.Folders
    Dim oItems As Outlook.Items
    Dim i As Long
    'Obtain a reference to deleted items folder
    Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems)
    Set oItems = oDeletedItems.Items
    For i = oItems.Count To 1 Step -1
        oItems.Item(i).Delete
    Next
    Set oFolders = oDeletedItems.Folders
    For i = oFolders.Count To 1 Step -1
        oFolders.Item(i).Delete
    Next
End Sub
found at
http://msdn.microsoft.com/en-us/library/bb960906(office.12).aspx
 

Premanshu

Board Regular
Joined
Oct 2, 2007
Messages
91
Hey Michael

i think this is the exact thing i was looking for...an ultimate solution for the issue i was facing.

Thank you sooooo very much for the help..

Regards,
Premanshu.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,635
Messages
5,512,528
Members
408,902
Latest member
VicRattlehead

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top