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
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

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
21,314
Office Version
  1. 365
  2. 2007
Platform
  1. 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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,109
Messages
5,857,441
Members
431,879
Latest member
KiwDaWabbit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top