Re: Trying to use Items collection to Delete
- From: BykrDan <BykrDan@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 18 Apr 2007 11:26:00 -0700
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
- References:
- Trying to use Items collection to Delete
- From: Mike G
- Re: Trying to use Items collection to Delete
- From: Mike G
- Trying to use Items collection to Delete
- Prev by Date: Re: remove custom add-in toolbars for editable email windows
- Next by Date: Re: run a vba procedure
- Previous by thread: Re: Trying to use Items collection to Delete
- Next by thread: Returning GAL DL members, then getting Office Location
- Index(es):
Relevant Pages
|