11
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!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kinkajou
  • 3,664
  • 25
  • 75
  • 128

3 Answers3

22

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
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • 4
    +1 though my own personal taste is assigning `olFolder` in one statement rather than overwriting it with a second statement: `Set olFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Temp")` – Jean-François Corbett Nov 30 '11 at 07:55
  • thx Jean-Francois. That is a good point, it is a quirk of my Outlook VBA coding that persists today, even though I code differently elsewhere in VBA and vbscript :) – brettdj Nov 30 '11 at 08:11
  • 2
    +1 When accessing non-default folders, you need to walk the hierarchy. Setting cascading object references to limit the number of dots is best practice, but when I'm in a hurry I will stuff everything into one line as @Jean-FrançoisCorbett does. – JimmyPena Nov 30 '11 at 18:44
  • @JP : who says setting cascading object references is "best practice"?? It makes absolutely no difference and is purely a matter of taste; my taste says it's better not to clutter code with the creation of useless objects. I could see the point if you were going to keep them to use them later (e.g. `olInbox`). But if you're not even going to use them, then your creating them is really just to suit your own taste, which is fine, but that doesn't make it "best practice". – Jean-François Corbett Nov 30 '11 at 19:35
  • 1
    If you don't want to do it, don't, but limiting the number of dots when accessing objects and/or properties is a known way to speed up code and is not simply a personal preference. – JimmyPena Nov 30 '11 at 19:56
  • What is `olFolderInbox`? –  Mar 09 '16 at 16:25
  • It is a method to directly access the Inbox folder – brettdj Mar 10 '16 at 04:17
  • Hi @brettdj, I am using the code suggested by you. But I am not able to access my subfolder inside Inbox. It keeps saying object not found! – Neels Feb 03 '17 at 04:11
  • @neels, suggest you ask a new question and provide full details. cheers – brettdj Feb 03 '17 at 04:43
  • Thank You. I figured it out anyhow. I was pointing to a wrong Outlook data file. – Neels Feb 03 '17 at 04:47
  • seems like a lot of variables, could do that all in one line. – Jorge Aug 18 '17 at 13:53
4

I found that there were some items in my inbox that were not mail items causing the script to halt. This little change allowed the script to keep running if something like a meeting invite is found:

Sub getmail()

Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder

'Dim msg As Outlook.MailItem
Dim InboxItem As Object

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

For Each InboxItem In olFolder.Items
    Debug.Print InboxItem.Subject
    Debug.Print InboxItem.EntryID
Next

End Sub

Thanks for your answer! helped me a lot!

(My apologies - wanted to comment, but don't have enough rep..)

Casey Morter
  • 123
  • 1
  • 9
4

And to drill further down, keep adding Set olFolder lines:

Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("temp")
Set olFolder = olFolder.Folders("temp2")
Set olFolder = olFolder.Folders("temp3")

Gets you to \Inbox\temp\temp2\temp3\

Greg Glynn
  • 171
  • 1
  • 3