PDA

View Full Version : Solved: CDO Method to delete items programmatically



Scottie P
06-12-2004, 01:11 AM
Hi All.

I was just wondering around the internet minding my own business when this code just jumped onto my clip board. Obviously curious, I tried it out - and it works great...for one item per click/run of the code.
I have no knowledge in Outlook VBA so I would like to know this:

Is there a way to have this work on a selection of multiple items?
For instance, I select 5 items in the folder [items that I want to see go away for good] and hit the little 'Wipe 'em out' button and all 5 go...not just one each press of the button/run of the code.

The code in question:


Sub DeleteSelectedItem()
Dim objApp As Outlook.Application
Dim objItem As Object
Dim objCDO As MAPI.Message
Dim objCDOSession As MAPI.Session
Dim strEntryID As String
Dim strStoreID As String

Set objApp = CreateObject("Outlook.Application")
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
If Not objItem Is Nothing Then
Set objCDOSession = CreateObject("MAPI.Session")
objCDOSession.Logon "", "", False, False
strEntryID = objItem.EntryID
strStoreID = objItem.Parent.StoreID
Set objCDO = objCDOSession.GetMessage(strEntryID, strStoreID)
If Not objCDO Is Nothing Then
objCDO.Delete
End If
End If

objCDOSession.Logoff
Set objCDOSession = Nothing
Set objItem = Nothing
Set objCDO = Nothing
Set objApp = Nothing
End Sub


Any takers?

X

mark007
06-12-2004, 03:55 AM
Try changing this:

Set objItem = objApp.ActiveExplorer.Selection.Item(1)

to

for each objItem in objApp.ActiveExplorer.Selection

then add a 'next' below the end if.

That should do it to all selected messages.

:)

Scottie P
06-12-2004, 09:52 AM
exactly, Mark!
performs wonderfully!

Regards,

Scott

mark007
06-12-2004, 11:32 AM
Good, good, good :)

brettdj
06-21-2004, 10:11 PM
Weird - the code is using early binding dimensioning with late binding


Dim objApp As Outlook.Application
Dim objCDO As MAPI.Message
Dim objCDOSession As MAPI.Session


Set objApp = CreateObject("Outlook.Application")
Set objCDOSession = CreateObject("MAPI.Session")


Cheers

Dave

mark007
06-22-2004, 01:24 AM
Yes this is odd:


Set objApp = CreateObject("Outlook.Application")

Could understand the use of getobject but not createobject. Should use:

Set objApp = New Outlook.Application