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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Inbox Loop 1

Status
Not open for further replies.

air1access

Technical User
Joined
Jan 27, 2008
Messages
123
Location
US
The code posted below works fine until it comes to something other then an Outlook "message"...

I need to have it go thru each item in the inbox, check to see if its a "message", and go from there.
If not, skip, and continue the loop...

Any suggestions or examples..? Thanks in advance..!!!

Sub Mx_Schedule()
Dim Message As MailItem

Set myNameSpace = Application.GetNamespace("MAPI")
Set MyInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

For Each Message In MyInbox.Items
If InStr(Message.Subject, "ScheduledWork") > 0 Or _
InStr(Message.Subject, "NewUnAssignedTaskNos") > 0 Or _
InStr(Message.Subject, "TaskNoEffectivity") > 0 Or _
InStr(Message.Subject, "ADs") > 0 Or _
InStr(Message.Subject, "HvyMxVisitTalley") > 0 Or _
InStr(Message.Subject, "WANCancellation") > 0 Or _
InStr(Message.Subject, "TaskCompliance") > 0 Or _
InStr(Message.Subject, "ScheduledWorkCompliance") > 0 Then
Message.SaveAs "D:\UPSDATA\" & Message.Subject & ".txt", OLTXT
Message.Delete
End If
Next Message

End Sub
 
What about this ?
Sub Mx_Schedule()
Dim Message As Variant
...
For Each Message In MyInbox.Items
If Message.Class = olMailItem Then
...
End If
Next Message
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

I tried your suggestion...
Its no longer errorring, but its not saving the email as a .txt file..
Any other fixes or suggestions..?

Thanks for your help..!!
air1access
 
Can you find what class works / fails, and what line it fails on?

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Its giving me a "type mismatch" error...
Fails on this line:
-- For Each Message In MyInbox.Items --

I'm testing with only 10 emails in my inbox. One of them is a is a "Meeting" item type. It fails when it comes to the "Meeting" item type - in this example its the first item in the loop... (side question: Is there any way to control the start position or loop sort order?).

Any user may have something other then a "Message" item type in their Outlook Inbox.

I hope this makes sense..!!!

Thanks for the help..!!
 

Are you sure it fails on the 'For Each...' line? If so try setting Message as a var
Code:
Dim Message [b]'[/b]As MailItem

You may then
Code:
debug.print Message.Class
to ID the message class that works and the one that doesn't. then test each Message before your code hits it.


Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Ah! I've just re-read the thread and notice that PHV already sugessed the same approach.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Thank you...!! that fixed it..!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top