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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,788
Members
449,049
Latest member
greyangel23

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
Back
Top