Loop through messages in inbox

E

Eric W.

I am coding for the first time using the Outlook object
model. I have used VBA in Excel and Access before, so I
do have some experience. So I need to loop through the
emails in my Inbox and look for the email to have an
attachment named "email Query.xls". I'm sure there is an
easy way to do this. I am coding from within Outlook.
Any help is welcome.
 
A

andrei

I had similar problem: to look for specific items inside the Inbox.
Enumerating all items is straightforward, but VERY slow. That's what I am
using:

Dim myNS As NameSpace
Set myNS = outlookApp.GetNamespace("MAPI")

Dim inFolder As Outlook.MAPIFolder
Set inFolder = myNS.GetDefaultFolder(olFolderInbox)

Dim msg ' do not put As MailItem!!!
For Each msg In inFolder.Items
If Not TypeOf msg Is MailItem Then
If msg.UnRead = True Then ' your criteria here
....
End If
End If
Next

There are several associated problems:

1. If the item is not MailItem, the loop is terminated. I learned it the
hard way;

2. Most of the properties are "protected" - Outlook is displaying a message
box, asking, whether to go ahead. To avoid that, you have to use
Redemption, which is presenting other problems;

3. Outlook inserts the new items either at the beginning or at the end of
the list. Unfortunately, I have no idea how to find out, which method is
being used. Sorting items has no effect on that. With large Inbox, lookup
is taking quite a while. If anybody can comment on that, would be
wonderful;

4. Do not expect the following statement to be always correct:
MsgBox "Inbox should contain " & _
inFolder.Items.Count & " items, of which " & _
inFolder.UnReadItemCount & " are unread."
On one PC, it is steadily reporting approx. twice as much items, and half
of them to be unread (instead of one). My only guess is trashed Inbox. But
you can't tell that to the customer, if he don't have other Outlook
problems. Because of that, I can not use indexing to enumerate items
backwards.

Overall, I am surprised by Outlook's instability and lack of decent online
documentation. You have to guess and poke around...

Any further comments greatly appreciated.

Regards,
Andrei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top