Outlook automation

M

MikeC

Michael,

Try this. I just tested it and verified that it works.

Dim objOutlook As Outlook.Application
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itms As Outlook.Items
Dim itm As Outlook.MailItem
Dim i As Integer
Dim strProfile As String
Dim strPwd As String

strProfile = "Your Outlook Profile Name" 'Not user ID
strPwd = "Your Password"

Set objOutlook = CreateObject("Outlook.Application")

Set nms = objOutlook.GetNamespace("MAPI")

'Log into email account.
nms.Logon strProfile, strPwd, False, True

'Initialize the Outlook items collection.
Set itms = nms.Session.GetDefaultFolder
(olFolderInbox).Items

'IMPORTANT: A count down loop is *required* for
processing each item in a
'collection. Otherwise, in a "For Each x in y" loop,
the counter skips every
'other item!!! Yes, it's hard to believe, but true.

'Check each item in the "InBox".
For i = itms.Count To 1 Step -1
If itms(i).Class = olMail Then
If itms(i).Subject = "Your Subject" Then
'Do something like display a message.
MsgBox itms(i).Subject
Else
'Do something else.
End If
End If
Next i
 
M

MikeC

Michael,

I have not experienced the count down issue using a "For
Each" loop when processing collections of forms or
tables. I've seen this problem only in Outlook. It's
possible that it may be lurking somewhere else, but I
haven't seen it.
 
R

Rick Brandt

AFAIK the need to count backward through a collection is only necessary if
you are also deleting items from the collection as you go.

If you loop through forward and delete item(0) then item(1) becomes
item(0). Your loop moves on to delete item(1) which is now the item that
was originally item(2) when the loop started (you skip every other one in a
similar fashion).

As long as you are not removing items from the collection then starting a
the beginning should work just fine.


--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
 

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