# Outlook 2003 Programming - Delete Items



## L-S-EBuss (Aug 5, 2004)

I would like to write a program or macro to delete messages in outlook. these messages are e-mails. the problem is:
1) e-mail in my public address book and my personal address book.
2) I do not want them to go to the deleted folder. 
I am using an MS Exchange 2003 server on a network.
Should I use CDO, VBA, ?
and how should I write the code.
Here is what I've tried:

////////////////////////////1st attempt with a macro (VBA)//////////////////////
Public Sub EmptyJunkEmailFolder1()

Dim outapp As Outlook.Application
Dim junkFolder As Outlook.MAPIFolder
Dim junkItem, deleteItem As Object
Dim entryID As String

Set outapp = CreateObject("outlook.application")
Set junkFolder = outapp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk)

For Each junkItem In junkFolder.Items
entryID = junkItem.entryID ' Store item entry id
junkItem.Delete ' Delete from junk mail folder

' To permanently delete item find and delete from deleted items
Set deleteItem = outapp.Session.GetItemFromID(entryID)
deleteItem.Delete
Next

Set junkItem = Nothing
Set deleteItem = Nothing
Set junkFolder = Nothing
Set outapp = Nothing

End Sub


/////////////////////////////////Second Attempt (VBA)//////////////////////
Public Sub EmptyJunkEmailFolder2()

Dim outapp As Outlook.Application
Dim fldJunk As Outlook.MAPIFolder
Dim olitem As Object


Set outapp = CreateObject("outlook.application")
Set fldJunk = outapp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk)

For Each olitem In fldJunk.Items
olitem.Delete
Next

Set fldJunk = Nothing
Set olitem = Nothing
Set outapp = Nothing

End Sub
///////////////////////////////////////////////////////////////////

You help is greatly needed and appreciated.


----------



## L-S-EBuss (Aug 5, 2004)

Can anybody help!!!!!


----------



## Rockn (Jul 29, 2001)

Does any of your code shown above work for emptying the junk email folder? If you get your macro to work just save it as an OTM file and you can assign a custom button to the macro. I would use VBA like you are doing.


----------



## L-S-EBuss (Aug 5, 2004)

This code does not work.
And yes, I did save it as an OTM.


----------

