Macro code to empty folders

D

Dave Pettit

From what I am reading, your first set of nested loops
always moves all of the junk mail to the delete folder,
but the second set of nested loops does not delete them
further.

If that is the case, would it be possible to redo your
Set statemeents after the first nested loops and before
the second nested loops?

---
Also, is there a difference in the kind of message that
is deleted last for the loops to stop? For example, if
the message has a priority flag set or an attachment.

--Dave
-----Original Message-----
The macro below deletes all of the items that get routed to a junk mail
folder and then deletes (empties) the Delete folder (OL 2003). Most of the
time it works just fine, but sometimes it will not empty the Delete folder.
Instead it may delete one item in the delete folder. Repeated runs will
delete addtional items one at a time or several at a time.

Anyone see what it is about this code that might contribute to this kind of
behavior?

One suggestion I've tried is following:

replacing these lines

For lngE = .Folders
(lngD).Items.Count To 1 Step -1
.Folders(lngD).Items(lngE).Delete
Next lngE

with this

With .Folders(lngD)
do while .items.Count > 0
.items(1).delete
Loop
End with

This works about the same as the original code below.

__________________________________

Sub ClearAllDeletedItems()
Dim olNS As Outlook.NameSpace
Dim collInfoStores As Outlook.Folders
Dim lngC As Long, lngD As Long, lngE As Long

Set olNS = Application.GetNamespace("MAPI")
Set collInfoStores = olNS.Folders

For lngC = 1 To collInfoStores.Count

With collInfoStores(lngC)
For lngD = 1 To .Folders.Count
If CBool(.Folders(lngD).Items.Count) And _
.Folders(lngD).Name = "Junk E-mail" Then
For lngE = .Folders
(lngD).Items.Count To 1 Step -1
.Folders(lngD).Items(lngE).Delete
Next lngE
End If

Next lngD
End With
Next lngC

For lngC = 1 To collInfoStores.Count

With collInfoStores(lngC)
For lngD = 1 To .Folders.Count
If CBool(.Folders(lngD).Items.Count) And _
.Folders(lngD).Name = "Deleted Items" Then
For lngE = .Folders
(lngD).Items.Count To 1 Step -1
 
C

Colonel Blip

Thanks Dave. That did the trick.

Dave Pettit said:
From what I am reading, your first set of nested loops
always moves all of the junk mail to the delete folder,
but the second set of nested loops does not delete them
further.

If that is the case, would it be possible to redo your
Set statemeents after the first nested loops and before
the second nested loops?

---
Also, is there a difference in the kind of message that
is deleted last for the loops to stop? For example, if
the message has a priority flag set or an attachment.

--Dave
(lngD).Items.Count To 1 Step -1
(lngD).Items.Count To 1 Step -1
(lngD).Items.Count To 1 Step -1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top