VB Snippet For Multiple Outlook Folders

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
colin.rogers
Newbie
Posts: 11
Joined: Sat Oct 28, 2017 11:49 am

VB Snippet For Multiple Outlook Folders

Post by colin.rogers » Fri Feb 16, 2018 11:57 pm

We are using the following VB code snippet to successfully attach a script to unread emails in the user's default inbox. This works very well.

Dim oOlAp
Dim oOlns
Dim oOlInb
Dim oOlItm
Dim oOlAtch
Sub LocateEmail
Set objOLApp = CreateObject("Outlook.Application")
If objOLApp is nothing then
MsgBox "Could not connect with (Outlook) ... exiting"
Exit Sub
End If
Set oOlns = objOLApp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(6)
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
MsgBox oOlItm.SenderEmailAddress
Next
Set objOLApp = Nothing
End Sub

However, we also now need to attach the script to other folders and inboxes listed in the user's Outlook. How would the code above differ to achieve this ?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: VB Snippet For Multiple Outlook Folders

Post by Marcus Tettmar » Mon Feb 19, 2018 8:07 pm

This is a Microsoft VBScript question. I Googled it for you and found these likely helpful articles:
https://msdn.microsoft.com/en-us/vba/ou ... od-outlook
https://msdn.microsoft.com/en-us/vba/ou ... on-outlook
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts