Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

REPOST: Looping thru inbox msgs

184 views
Skip to first unread message

vbro...@my-deja.com

unread,
Apr 10, 2000, 3:00:00 AM4/10/00
to
I am working on an application that is supposed to loop through all the
e-mail messages in a user's inbox in Outlook, check for
attachments and then move the message to some other folder depending on
certain criteria. The problem is that although the loop works, it
doesn't actually check ALL the messages - it exits the loop without an
error even though there are still messages in the inbox. I did a
search on past posts to this newsgroup, and Dev Ashish posted some code
from a class module that he wrote. I didn't use the class module
itself, but I did set up my loop the same way he did (i.e., using the
objItems.GetFirst...objItems.GetNext structure). Once again, the code
runs without generating any errors, but it still exits the loop before
it has checked all the messages. Has anybody figured out how to
prevent this? Or am I stuck telling my customer that they just have to
click the Check Mail button several times in a row to make sure there
are no messages? Appreciate all the help...
-Victoria


Sent via Deja.com http://www.deja.com/
Before you buy.

Danny J. Lesandrini

unread,
Apr 10, 2000, 3:00:00 AM4/10/00
to
Are you using On Error Resume Next, to ensure that errors
don't throw you out of the loop?

--

Danny J. Lesandrini
dan_n...@yahoo.com

<vbro...@my-deja.com> wrote in message news:8cr80a$cd0$1...@nnrp1.deja.com...

Dev Ashish

unread,
Apr 10, 2000, 3:00:00 AM4/10/00
to
ALso, if the code always stopping at a particular msg? If so, then check the
msg itself and make sure that all the fields that you are looking at in code
are actually populated. Email msgs with no "To:" field filled out, for
example, may cause the code to abruptly quit at that particular msg.

-- Dev

vbro...@my-deja.com

unread,
Apr 10, 2000, 3:00:00 AM4/10/00
to
Thanks for the replies. I tried Danny's suggestion of using on Error
Resume Next, but there are no errors. And as Dev suggested, it's not
any particular message that stops the loop. Strangely, it almost seems
that it will only look at about half the messages, no matter how many
are in the inbox. Just a few minutes ago, for example, I had 7
messages in the inbox. I clicked the button the first time, and it
checked 4 out of the 7. When I clicked it again, it checked 2 out of
the remaining 3. Also, I'm not actually looking at any fields in code,
simply whether or not the message has any attachments by a particular
name. Any other suggestions? Thanks...

In article <8cshpu$6uufh$1...@fu-berlin.de>,

Bri

unread,
Apr 11, 2000, 3:00:00 AM4/11/00
to
Hi there,

It sounds like the problem is with the loop not the message reading
code. It looks like you are processing every second message. Check for
anything that might affect the loop counter. This might be a dumb
question but do you use Next as well as i=i+1 in the loop? or anything
that changes the loop counter variable? If this isn't the problem you
might consider posting the actual code you are using so that everyone
can see exactly what you are doing.

Bri

vbro...@my-deja.com

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
Hey, thanks for the replies. Here is the code I've been trying to
use. It doesn't use a counter, so it doesn't seem that it could be
skipping every other message as suggested by Bri. If anyone can see
why this code would fail to check ALL messages in the inbox, pleeeeease
let me know. Thanks... Here it is:

Public Function CheckAttachments()

On Error Resume Next

Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objItems As Outlook.Items
Dim objMail As Object
Dim lngRetVal As Long
Dim intRecCount As Integer
Dim objAttachments As Outlook.Attachments
Dim objMessage As Outlook.MailItem
Dim SigningInbox As Outlook.MAPIFolder
Dim intCounter As Integer
Dim intItemCount As Integer

'check to see if Outlook is already open
lngRetVal = FindWindowByClass("rctrl_renwnd32", 0&)

'if Outlook is open, get the existing object, otherwise create new
object
If lngRetVal <> 0 Then
Set objOutlook = GetObject(, "outlook.application")
Else
Set objOutlook = CreateObject("outlook.application")
End If

'create objects to find a mail item
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objItems = objNameSpace.GetDefaultFolder(olFolderInbox).Items

'intItemCount = objItems.Count

Set objMail = objItems.GetFirst
Do While Not objMail Is Nothing
objMail.Display
Set objMessage = objOutlook.ActiveInspector.CurrentItem
Set objAttachments = objMessage.Attachments

If objAttachments.Count = 0 Then
'move the message to the appropriate folder
Else
'depending on name of attachment, call import function

End If
Set objMail = objItems.GetNext
Loop


Set objAttachments = Nothing
Set objMessage = Nothing
Set objMail = Nothing
Set objItems = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing

End Function


In article <38F3523F...@here.com>,


Bri <n...@here.com> wrote:
> Hi there,
>
> It sounds like the problem is with the loop not the message reading
> code. It looks like you are processing every second message. Check for
> anything that might affect the loop counter. This might be a dumb
> question but do you use Next as well as i=i+1 in the loop? or anything
> that changes the loop counter variable? If this isn't the problem you
> might consider posting the actual code you are using so that everyone
> can see exactly what you are doing.
>
> Bri
>

Bri

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Hi there,

Wow, my news server must not like you. Your message, dated Apr 14,
didn't show up on my server until today, Apr 19.

I think I see what might be happening. When you 'move' the message to
another folder then there is no record for the curser to sit on anymore
so it moves to the next record. You then issue a 'GetNext'. Since it is
already on the next message it skips it and you get the second one. Try
commenting out the 'GetNext' to see if this is the problem.

This is just a guess you realize, but it is the only explaination that I
can come up with without trying the code out myself.

Bri

vbro...@my-deja.com

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
Bri -

A day or two after I posted the code, I actually thought that moving
the message is probably what's causing the problem. Unfortunately,
though, commenting out the GetNext doesn't fix it. When I comment it
out, it actually displays the message it just supposedly moved again
and again, and somehow puts multiple copies of the same message into
the new folder. So, it's gone from the inbox, but it still considers
that message to be the active item. The other thing I noticed is that
it is not consistent as to which message comes up as the GetFirst. I
keep my messages in descending order by receipt date (so most recent
first), and sometimes it goes to the last message in the inbox first,
and sometimes it picks a message in the middle of the inbox. Even when
it picks the last message in the inbox first, it still manages to check
a few more messages and then exits the loop w/ out error. At this
point, I'm totally confused and frustrated because it seems that the
behavior is not consistent. If you have any other thoughts on this,
I'd really appreciate it because I would hate to think I'm going to
have to tell my user, "Oh, it's OK, just hit the button a few times to
make sure you got all the messages." Gee, that's really
professional... Once again, thanks for the help! :-) Victoria

In article <38FDCE9A...@here.com>,


Bri <n...@here.com> wrote:
> Hi there,
>

Bri

unread,
Apr 21, 2000, 3:00:00 AM4/21/00
to
Victoria,

How about trying a slightly different approach? Instead of moving the
message to the folder, just copy it. If all of the messages are now
processed then it is the move that is causing the problems. There might
be another way to do the move that behaves differently.

An immediate solution would be to enclose your loop in another loop that
tests for zero messages in the inbox. This would process all of the
messages in multiple passes. It still seems like a strange scenario, but
this would kludge it into working.

Of course the best thing would be to identify why the move both leaves
the pointer on the moved message and skips one when you move to the next
message. Since I have not yet written any code to process Outlook
messages, I don't have any first hand experience that would provide more
insight. I only can conclude that there is something happening in the
loop (skipping records) from the symptoms you described. One of these
days I will have to write similar code (a client has hinted at wanting
to unify their Access Contact list with their Outlook Contact List) so I
will probably find myself in the same place you are in now. If you do
eventually find the real glitch, please post the solution. In the mean
time you can still deliver the desired behavior (from the users
perspective) by using the kludge above.

Hope this helps, good luck.

Bri

vbro...@my-deja.com

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Bri,

Thanks for all your help. Once again it seems we are thinking along
the same lines (i.e., "tricking" Outlook into checking all the
messages). I'll give that a try this afternoon. And, of course, if I
find the "real" solution, I will post it here. I'm not holding my
breath though... :-)


In article <39008922...@here.com>,

Bri

unread,
Apr 28, 2000, 3:00:00 AM4/28/00
to
Victoria,

I was following another thread and in it Michka pointed out something
that is the solution to your problem. As I suspected, moving the message
(the equivelent of 'closing the Form' in the other thread) confuses the
pointer. Michka's solution is to use a FOR/NEXT loop that loops
BACKWARDS through the items so that the pointer is always moving records
whose position hasn't been altered by the close (or move in your case).
So:

CODE START
==========

for i= intItemCount To 1 step -1 ' was: Do While Not objMail Is


Nothing
objMail.Display
Set objMessage = objOutlook.ActiveInspector.CurrentItem
Set objAttachments = objMessage.Attachments

If objAttachments.Count = 0 Then
'move the message to the appropriate folder
Else
'depending on name of attachment, call import function
End If

' no longer required: Set objMail = objItems.GetNext
next ' was: Loop

CODE END
========

Give this a shot. I have pasted in the message from the other thread for
you below.

Bri.

=======================================
Subject: Re: Using DAO to Step Through Forms
Date: Thu, 27 Apr 2000 05:56:07 -0700
From: "Michael \(michka\) Kaplan"
<forme...@spamfree.trigeminal.nospam.com>
Organization: Trigeminal Software, Inc.
Newsgroups: comp.databases.ms-access

No, you should not do this, as it will not close all the forms, since
the
iterator loses its place and closes every other form. You should count
backwards in a For...Next loop.

--
MichKa
"Cause it's a bittersweet symphony, thats life..." -- The Verve

random junk of dubious value, at the multilingual,
no scripts required, http://www.trigeminal.com/

"Wolfgang Stein" <wolfgan...@gabo.de> wrote in message
news:sggcme...@corp.supernews.com...
> Hi,
> try this:
>
> Dim frm As Form
>
> DoCmd.Close 'Closes form with the focus
>
> For Each frm In Forms
> If frm.Visible = False Then
> DoCmd.Close acForm, frm.Name
> End If
> Next
>
> Josh wrote:
> >
> > Hi,
> >
> > I have 3 forms that I toggle between Visible and not Visible. Only 1
> > form has it's visible property set to true at any given time, but
> > neither, either, or both of the other forms can be open but invisible.
> > On each form, I have an exit button that I need to close the visible
> > form and any open, but invisible forms.
> >
> > My logic was to loop through each form in the database, and if the
> > visible property is false, close the form.
> >
> > I attempted the following code on the click event of the exit button:
> >
> > Dim frm as Form
> >
> > Docmd.Close 'Closes form with the focus
> >
> > 'Meant to close open, but invisible forms, but doesn't work.
> > For Each frm In CurrentDb().Containers!Forms
> > If frm.Visible = False Then
> > DoCmd.Close acForm, frm
> > End If
> > Next
> >
> > Unfortunatly, I get an error saying that "Operation is not supported
> > for this type of object". Is my syntax correct? Can anyone think of
> > an easier way to handle this?
> >
> > TIA,
> > Josh

vbro...@my-deja.com

unread,
Apr 28, 2000, 3:00:00 AM4/28/00
to
Hi, Bri,

*****IT WORKS!!!!!!!!*******
*****IT WORKS!!!!!!!!*******
*****IT WORKS!!!!!!!!*******

At first I tried it and it didn't work, but it turned out it was
because the objItems.Item(index) is 1-based NOT zero-based as I had
originally tried. I can't believe it actually worked!! Hooray!!

Here is the new code:
========
intItemCount = objItems.Count
For intCounter = intItemCount To 1 Step -1
Set objMail = objItems.Item(intCounter)

objMail.Display
Set objMessage = objOutlook.ActiveInspector.CurrentItem
Set objAttachments = objMessage.Attachments

If objAttachments.Count = 0 Then
'move the message to the appropriate folder
Else

'import and move
End If
Next
==========

Thanks again!!

-Victoria

In article <3909D86E...@here.com>,


Bri <n...@here.com> wrote:
> Victoria,
>
> I was following another thread and in it Michka pointed out something
> that is the solution to your problem. As I suspected, moving the
message
> (the equivelent of 'closing the Form' in the other thread) confuses
the
> pointer. Michka's solution is to use a FOR/NEXT loop that loops
> BACKWARDS through the items so that the pointer is always moving
records
> whose position hasn't been altered by the close (or move in your
case).
> So:
>
> CODE START
> ==========
>

> for i= intItemCount To 1 step -1 ' was: Do While Not objMail Is


> Nothing
> objMail.Display
> Set objMessage = objOutlook.ActiveInspector.CurrentItem
> Set objAttachments = objMessage.Attachments
>
> If objAttachments.Count = 0 Then
> 'move the message to the appropriate folder
> Else
> 'depending on name of attachment, call import function
> End If

> ' no longer required: Set objMail = objItems.GetNext
> next ' was: Loop
>
> CODE END
> ========
>
> Give this a shot. I have pasted in the message from the other thread
for
> you below.
>
> Bri.
>

0 new messages