We help IT Professionals succeed at work.
Get Started

Deleting Items From a Public Folder using VB Script

gcz asked
Last Modified: 2012-05-07

Bit of a weird one. I have written a basic script (below), which checks a public folder called 'Invoices Out' and deletes any older than 90 days.

Problem is, the script only seems to delete half of the items eligible for deletion. For example, the first time I ran it, it deleted 283 items out of 565. Next time I ran it, exactly the same script, it deleted 141, then 71, then 35, then 18, then 9. I now have 9 items left in, and I'm sure if it ran again, it would delete 4 or 5!

Am I missing something? The script actually only echoes the items it is actually deleting, so it looks like the 'If' statement is returning false for some reason (as opposed to the delete command failing). The sleep command I added to check if the speed of the loop was an issue but it didn't make any difference.

Any ideas?
dtmCutOff = DateAdd("d",-90,Date)
Set objOLK = CreateObject("Outlook.Application")
Set objNS = objOLK.Application.GetNamespace("MAPI")
Set objPF = objNS.Folders("Public Folders").Folders("All Public Folders").Folders("Invoices Out")
For Each objItem In objPF.Items
	If DateDiff("d",dtmCutOff,objItem.receivedTime) <= 0 Then 
		WScript.Echo "DELETING : " & objItem.subject & " " & objItem.receivedTime
		intDeleted = intDeleted + 1
		WScript.Sleep 500
	End if
WScript.Echo "Total " & intDeleted & " items deleted."
'Clean up
Set objOLK = Nothing
Set objNS = Nothing
Set objPF = Nothing

Open in new window

Watch Question
Head of ICT
Top Expert 2009
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE