Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

button to delete Junkmail using VBA 1

Status
Not open for further replies.

d1novak

Instructor
Jul 21, 2003
27
US
I have created a button in outlook 2003 utililizing the following VBA code:

Public Sub EmptyJunkEmailFolder()

Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Dim olitem As Object
Dim fldJunk As Outlook.MAPIFolder
Dim gone As Date
gone = Date

Set fldJunk = NS.GetDefaultFolder(olFolderJunk)
For Each olitem In fldJunk.Items
If olitem.ReceivedTime < gone Then
olitem.Delete
End If
Next

Set fldJunk = Nothing
Set olitem = Nothing
Set NS = Nothing

End Sub

The problem is it seems to only do a few at a time. I have to click it numerous times before it stops deleting as the only remaining emails are toddays.

I am slightly confused...
Please help.
 
Perhaps something like this ?
For i = fldJunk.Items.Count - 1 To 0 Step -1
With fldJunk.Items(i)
If .ReceivedTime < gone Then
.Delete
End If
End With
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
When you delete items from a collection, you usually have to delete from the end backwards, so try something like

Dim x as long
For x = fldJunk.Items.Count to 1 step -1
set olitem = fldJunk.Items(x)
If etc....
Endif
Next

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top