Re: Trying to use Items collection to Delete

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Mike, thank you for posting this. A very similar problem has been
frustrating me for about a month now; I tried both For|Each and looping an
index - but never thought to loop backward!

Dan.

"Mike G" wrote:

Nevermind, I figured it out, you can't use for each, you must iterate
backwards through the collection.

See: http://support.microsoft.com/?kbid=222480

Regards,

Mike

Hi -

This is starting to get very frustrating! What I am attempting, seems to
me to be very straightforward, and should be a simple task! I have tried
this first using JScript (and Enumerator objects), thinking that there may
be a problem with Enumerators, I switched to using VBScript. Here is what
I want to do:

1. Scan through my folders until I find a specific folder
2. Scan through that folder until I find the Inbox
3. Scan the Inbox of that folder for all MailItems and for each MailItem
with a specific string I want to:
a. Harvest some information from the body
b. Delete the mail.

Ok, so here is what I have:

set WSHShell = WScript.CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
set WSHNetwork = WScript.CreateObject("WScript.Network")

csvfile = "C:\\Work\\eMailAlerts\\eMail_Alert_Log.csv"

WScript.echo ("CSV File will be: " & csvfile)

if (objFSO.FileExists(csvfile)) then
set objCSV = objFSO.OpenTextFile(csvfile, ForAppending, false)
else
set objCSV = objFSO.OpenTextFile(csvfile, ForWriting, true)
sOutline = "Date,Server,Type,Element,AlertText"
objCSV.WriteLine(sOutline)
end if

set myOlApp = WScript.CreateObject("Outlook.Application")
WScript.echo ("myOlApp=" + myOlApp.Name)
set myNameSpace = myOlApp.GetNamespace("MAPI")
WScript.echo ("myNameSpace=" + myNameSpace)

for each oFolder in myNameSpace.Folders
WScript.echo ("oFolder.Name=" + oFolder.Name)
sfoldername = oFolder.Name
if (inStr(sfoldername,"~Support") > 0) then
WScript.echo ("found ~Support")

for each oITFolder in oFolder.Folders
sITFolder = oITFolder.Name
WScript.echo (sfoldername & "/" & sITFolder)

if (oITFolder.Name = "Inbox") then
ct = 0
mc = 0

for each oItem in oITFolder.Items
mc = mc + 1
sSubject = oItem.Subject

if (instr(sSubject, "Alert") > 0)
then
sBody = oItem.Body

call ProcessAlert (sBody,
objCSV)
ct = ct + 1

WScript.echo ("Deleting
item " & ct & " from " & oItem.SenderName)
oItem.Delete()

end if
Next ' oItem

WScript.echo ("Found " & mc & " Total
Messages and " & ct & " Alerts")

end if

Next ' oITFolder

end if
Next ' oFolder

objCSV.Close()

If I comment out the 'oItem.Delete', I see about 230 alerts. If uncomment
the delete, I only see about half of them! It appears that the delete is
shifting the remaining collection up by one, and the loop is getting the
next item (skipping one item with each delete).

I would expect for...each to retain some semblance of order and take into
account the deleted item when it iterates over the collection. It does
not appear to have that capability.

How can a routine be written to provide this capability?

Thanks,

Mike





.



Relevant Pages

  • Re: Terminal Server and %WINDIR%
    ... "Mike U." wrote in message ... > Hello Jason and Herb, ... >> variable to the user Windows folder. ... >> Microsoft Online Partner Support ...
    (microsoft.public.windows.server.migration)
  • Re: CNET.com
    ... I'd then run a thorough check for hijackware, Mike. ... Removing Trojans and Trojanware with Sysclean ... a desktop folder). ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: Windows XP Content Advisor
    ... CurrentVersion\Policies\rating folder to a floppy disk and named it. ... "Nepatsfan" wrote: ... >> Mike B ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Cannot Open any Applics after 2004 Install
    ... I know how frustrating this must ... Go to ~/Library/Fonts move any fonts found in this folder to a new ... Go to ~/Library/Caches/ and delete all Microsoft Folders ... Start up Activity Monitor (in Applications/Utilities) and do a File>Save ...
    (microsoft.public.mac.office)
  • Re: No Outlook Express
    ... >>> try to launch OE and a small window comes up looking for OE. ... >>> deleted everything in the OE folder. ... Is there a way to reinstall Outlook ... > And are you going to help Mike by telling him how to do it? ...
    (microsoft.public.windowsxp.general)