Macro's sporadic performance

C

Colonel Blip

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.

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

Thanks.
--
Colonel Blip
(e-mail address removed)
Remove "nospam" when replying.
__________________________________

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
.Folders(lngD).Items(lngE).Delete
Next lngE
End If

Next lngD
End With
Next lngC

Set collInfoStores = Nothing
Set olNS = Nothing
End Sub
 
J

Jonathan West

You might want to re-post this question to one of the groups on Outlook.
This group deals with VBA in Word.
 
J

Jezebel

Try 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
 
C

Colonel Blip

Peculiar, but it still has a problem, though it appears to be slightly
different. I find now I often have to run the macro twice to empty the
delete folder. 1st time nothing happens; 2nd time the folder is completely
emptyied.
 

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