Deleting messages

G

Guest

I have written a VBScript program to delete messages in Outlook infected with the Swen virus (I get up to 1200 per day). For some reason the program deletes roughly half the infected messages. Fortunately, I have the program log the messages it deletes, and I tested beforehand with the delete statement commented out. In testing (with delete commented out), all infected messages were logged. When I put the delete statement in, the program deletes 15 or 16, then skips 14 or 15 infected messages, deletes another 15 or 16, etc. Does anyone have any ideas? Should I add a pause where I loop through the messages (say with "Wscript.Sleep 400")? Should I loop in reverse order? Below is psuedo code demonstrating what I am doing.

' Log file name.
strLogFile = "c:\Swen.log"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile(strLogFile, 8, True, 0)
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
lngTotal = 0

Call EnumFolders(objNameSpace)

objLogFile.WriteLine "Infected messages deleted: " & lngTotal
objLogFile.WriteLine Now & " - Finished"
objLogFile.Close

Sub EnumFolders(objNS)
For Each objFolder In objNS.Folders
If UCase(objFolder.Name) = "INBOX" Then
For Each objItem In objFolder.Items
blnInfected = False
If (... test 1 ...) Then
blnInfected = True
objLogFile.WriteLine "Type 1;" & objItem.ReceivedTime
End If
If (... test 2 ...) Then
blnInfected = True
objLogFile.WriteLine "Type 2;" & objItem.ReceivedTime
End If
If blnInfected = True Then
objItem.Delete
lngTotal = lngTotal + 1
End If
Next
End If
Call EnumFolders(objFolder)
Next
End Sub

Thanks for any help or insight. I have Outlook 2000.

Richard Mueller
Microsoft MVP Scripting and ADSI
 
S

Sue Mosher [MVP]

Never delete inside a For Each loop. Instead, use a countdown loop

intCount = objFolder.Items.Count
For i = intCount to 1 Step -1
Set objItem = objFolder.Items(i)
' continue processing i
Next
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 

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