Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapi Question

Status
Not open for further replies.

prinand

MIS
Jun 13, 2000
98
GB
I have this script which should step thru all messages in a mailbox and process them and then delete it until the mailbox is empty
I put in a bunch of messages in the inbox and there were some warnings about the mailbox size in the inbox too.

When it exists the loop it turns out that usually there are still some messages left. also the error does not come at the end of the loop (oMessages.getnext) where I would expect the error when trying to skip beyond the last message but when trying to read the text body of the e-mail, and the mailbox is not empty sometimes, and the message is having text in the body of the e-mail

here is the script

Set oSession = CreateObject("MAPI.Session")
oSession.Logon "profile","",False,True,-1,True

Set oFolder = oSession.Inbox
Set oMessages = oFolder.Messages

set oMail = oMessages.getfirst
If Err <> 0 then
msgbox("error opening mailbox")
logfilefs.close
Wscript.quit
end if
Do
StrBody = oMail.text
If Err <> 0 then
msgbox("error textbody " & err.number & " - " & Err.description)
Else
If Instr(StrBody,"Backup Operation") <> 0 Then
'***********************
'*** process message ***
'***********************

oMail.Delete
Set oMail = Nothing
Else
'not a backup message ignoring entry
oMail.Delete
set oMail = Nothing
End If
End If
StrBody = ""
Set oMail = oMessages.getnext
Loop Until Err <> 0
Err.Clear
logfilefs.writeline("end of Loop. closing paramters")


so : what is the proper way to detect if I am at the last message ?

second question : why does the error occur at the : StrBody = oMail.text part while the mailbox is not empty ??? (not sure but I got the impression it was giving the error at oMail.text when processing the mailbox size warnings), but next time I would execute the script, it would process it (just delete it) and move on... after 3 times executing the script, the mailbox is realy empty....

Question 3 : why do I not process all the messages in one go, how come it stops at this error while the mailbox is not empty
 
As you use the Delete method I suggest the GetLast and GetPrevious method until oMail Is Nothing

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, I tried the :
do while NOT oMail is Nothing
test and indeed it does no longer throw an error and exits the loop properly

following your suggestion about the getlast, I read the CDO helpfile, and found out they suggest to

oMail = oMessage.getfirst
Do while NOT oMail is Nothing
***** process it
oMessage.Item(1).delete
oMail = oMessage.Item(1) (or oMessage.getfirst)
Loop

apparently a delete refreshes the count and pointers and SO : you were pointing at item(1) getnext causes it to move to item(2) but due to the delete, it refreshes, and the message that was at item(2) becomes item(1) so since the getnext moves to item(2) you skip item(1) so my feeling was right that only half of the messages was processed. every other message is ignored in my script

I could not specificly find this but I presume this also is happening when moving files to another folder instead of deleting, as that was what I was doing initially when I ran into the issue that not all messages were processed

 
I think there are places where error control is unnecessary. And there are proper way to loop through and delete items from the collection which is enumerated, else unexpected result may occur as op noted.

This is how it can be done more concisely.
[tt]
Set oSession = CreateObject("MAPI.Session")
oSession.Logon "profile","",False,True,-1,True

Set oFolder = oSession.Inbox
Set oMessages = oFolder.Messages

for i=oMessages.count to 1 step -1
StrBody = oMessages.item(i).text
If Instr(StrBody,"Backup Operation") <> 0 Then
'***********************
'*** process message ***
'***********************
oMessages.item(i).Delete 'or some other action
Else
'not a backup message ignoring entry
oMessages.item(i).Delete
End If
next
set oMessages=nothing
set oFolder=nothing
oSession.logoff
set oSession=nothing

logfilefs.writeline("end of Loop. closing paramters")
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top