Question about CDO 1.21

  • Thread starter Thread starter Bob Smith
  • Start date Start date
B

Bob Smith

I have a real simple function that all it does is Delete messages older than
X days. I'm not sure why, but if I run it once it deletes some of the
messages but not all. With 1000's of messages in the folder it may take 3 or
4 runs for it to finally delete all the messages over X days. Is there some
problem with CDO where it can only process a small number of messages at one
time?

Dim NumDaysToDelete

NumDaysToDelete = 3

CleanMail(NumDaysToDelete)

Sub CleanMail
Dim oSession
Set oSession = CreateObject("MAPI.Session")

strProfileInfo = "cgyex001" & vblf & "ecntrckm"

oSession.Logon "", "", False, True, 0, True, strProfileInfo

Const CdoDefaultFolderInbox = 1 ' uncomment for VBScript code

Dim oFolder
Set oFolder = oSession.GetDefaultFolder(CdoDefaultFolderInbox)

Dim oMessages
Set oMessages = oFolder.Messages
For Each oMessage In oMessages
If DateDiff("d",oMessage.TimeReceived,now) >= NumDaysToDelete Then
oMessage.Delete
End if
Next
End Sub
 
This has nothing to do with CDO and everything to do with using a For...Each
loop. That and a count up For loop establish an index counter into the
collection of items and every time you delete an item it messes with the
index. Usually that results in deleting 1/2 of the items within the loop.

Use a count down For loop (Step -1) or a Do loop that checks for count of
items instead and it should work as expected.
 
Thanks for your help. I guess I'm still having trouble trying to get this to
work. I don't know the number of items to be deleted until I have evaluated
the Receieved time. I tried a Do While Not oMessage Is Nothing Loop, but
still run into the same problem. I also checked the CDOLive website, but I am
still having problems coming to terms with the concept. Any chance you can
guide me as to what I am doing wrong.
 
Got it working, thanks for your help.


Ken Slovak - said:
This has nothing to do with CDO and everything to do with using a For...Each
loop. That and a count up For loop establish an index counter into the
collection of items and every time you delete an item it messes with the
index. Usually that results in deleting 1/2 of the items within the loop.

Use a count down For loop (Step -1) or a Do loop that checks for count of
items instead and it should work as expected.
 
Back
Top