Error when cycling through inbox items with VB programs.

G

Greg

I am working on a program to get the attachments from items in my
inbox. I know that there are 501 items in my inbox.

However, after looking at the 316th item, I get a type mismatch at the
"Next MyMail" line.

Can anyone shed any light on this? Here is my code to recreate the
error.

Is there any other type of item other than a MailItem that is in the
501 count that could be causing the problem?

Private Sub GetAttachments()
Dim OL As Outlook.Application
Dim OLNS As Outlook.NameSpace
Dim InBox As Outlook.MAPIFolder
Dim MyMail As MailItem
Dim MyAttachment As Attachment
Dim numAttachments As Integer
Dim numItems As Integer

Set OL = New Outlook.Application
Set OLNS = OL.GetNamespace("MAPI")
Set InBox = OLNS.GetDefaultFolder(olFolderInbox)

numItems = 0
numAttachments = 1
MsgBox InBox.Items.Count & " items in inbox"

For Each MyMail In InBox.Items
numItems = numItems + 1
txtData.Text = txtData.Text & vbCrLf & numItems
txtData.SelStart = Len(txtData.Text)

Next MyMail

Set OL = Nothing
Set OLNS = Nothing
Set InBox = Nothing
Set MyMail = Nothing
Set MyAttachment = Nothing

' Unload Me
End Sub

Thanks.

Greg
 
S

Sue Mosher [MVP-Outlook]

Not every item in your Inbox is necessarily a MailItem. Declare MyMail as Object and throw in an On Error Resume Next statement.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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