Reference a folder by name

28,312

Solution 1

Say under the Inbox was a folder named Clients and under that was a folder named Vijay Baswal

Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Clients").Folders("Vijay Baswal")

OlDefaultFolders Enumeration http://msdn.microsoft.com/en-us/library/office/bb208072(v=office.12).aspx

The Inbox is olFolderInbox or 6. Appears there is no 37.

Solution 2

see below vba snippet to check how to read mail from specific folder

 Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant
Dim outFolder As Outlook.Folder

 Dim olItem As Outlook.MailItem


Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("folder1").Folders("fol2")
Set olItms = olFldr.Items


olItms.Sort "Subject"

i = 1

For Each olItem In olItms
    'If InStr(olMail.Subject, "Criteria") > 0 Then

       Dim szVar As String
      szVar = olItem.Body
        szVar1 = olItem.Subject
        i = i + 1
    'End If
Next olItem

Set olFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
Share:
28,312
Zelig
Author by

Zelig

Updated on June 21, 2020

Comments

  • Zelig
    Zelig almost 4 years

    I need to get a folder by name, not by folder number counts. I tried getting with various methods.

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    'Dim OlFolder As Outlook.MAPIFolder
    Dim objFolder As Outlook.Folder
    Dim myolItems As Outlook.Items
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    'Set myOlItems = objNS.GetDefaultFolder(37).Folders("Vijay Baswal").Items
    'Open the folder
    Set objFolder = olApp.Session.GetDefaultFolder("Vijay Baswal")