Using visual basic to access subfolder in Inbox?

kinkajou picture kinkajou · Nov 30, 2011 · Viewed 64.1k times · Source
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items

I have used the code above to access the main outlook Inbox but how to access the folders in inbox and it's mail using vba!

Answer

brettdj picture brettdj · Nov 30, 2011

Thats very close :)

To get all the mail items in a folder called "temp" under the Inbox try this

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("Temp")

For Each msg In olFolder.Items
    Debug.Print msg.Subject
Next