• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Working with VBA and non-default Outlook Folders

Slipstick Systems

› Developer › Working with VBA and non-default Outlook Folders

Last reviewed on May 29, 2024     392 Comments

This code sample uses a default Outlook folder:

Sub Whatever()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")

'use the default folder  
Set Items = Ns.GetDefaultFolder(olFolderCalendar).Items

'do whatever

End Sub

To use a folder at the same level as the Default folders (such as Calendar, Inbox etc), use this in place of Set Items = Ns.GetDefaultFolder(olFolderCalendar).Items, where SharedCal is the folder name:

Set Items = Session.GetDefaultFolder(olFolderCalendar).Parent._
     Folders("SharedCal").Items

When the folder is a subfolder under the default Calendar folder, use this instead:

Set Items = Session.GetDefaultFolder(olFolderCalendar)._
  Folders("SharedCal").Items

To use the currently selected folder, you'll need to use:

Set Items = Application.ActiveExplorer.CurrentFolder.Items

Use a folder in another pst or mailbox

To use a specific folder in another data file in the profile, you need to use a function. Call the function in your macro in this manner, where GetFolderPath is the function name:

Set Items = GetFolderPath("New PST\Test Cal").Items

Folder paths

After adding the function to ThisOutlookSession:

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
    Dim oFolder As Outlook.Folder
    Dim FoldersArray As Variant
    Dim i As Integer
        
    On Error GoTo GetFolderPath_Error
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set oFolder = Application.Session.Folders.item(FoldersArray(0))
    If Not oFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = oFolder.Folders
            Set oFolder = SubFolders.item(FoldersArray(i))
            If oFolder Is Nothing Then
                Set GetFolderPath = Nothing
            End If
        Next
    End If
    'Return the oFolder
    Set GetFolderPath = oFolder
    Exit Function
        
GetFolderPath_Error:
    Set GetFolderPath = Nothing
    Exit Function
End Function

Use a shared folder (Exchange mailbox)

To access a shared folder in another user's Exchange server mailbox, you need to use GetSharedDefaultFolder to reference the mailbox, after resolving the address to the folder. If the shared folder was opened from a sharing invitation (or Open Other User's folder command), you may need to use the method in the next section.

You can use the mailbox owner's display name, alias, or email address when resolving the recipient.

  Dim NS As Outlook.NameSpace
  Dim objOwner As Outlook.Recipient
   
  Set NS = Application.GetNamespace("MAPI")
  Set objOwner = NS.CreateRecipient("maryc")
    objOwner.Resolve
       
 If objOwner.Resolved Then
   'MsgBox objOwner.Name
 Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
 End If

To add an item to a folder in a shared mailbox, use Items.add:

Set objAppt = newCalFolder.Items.Add(olAppointmentItem)

 

Default Folder Names

The following are the default folder names to use when referencing Outlook's default folders.

NameValueDescription
olFolderCalendar9Calendar folder.
olFolderContacts10Contacts folder.
olFolderDeletedItems3Deleted Items folder.
olFolderDrafts16Drafts folder.
olFolderInbox6Inbox folder.
olFolderJournal11Journal folder.
olFolderJunk23Junk E-Mail folder.
olFolderNotes12Notes folder.
olFolderOutbox4Outbox folder.
olFolderSentMail5Sent Mail folder.
olFolderTasks13Tasks folder.
olFolderToDo28To Do folder.
olFolderRssFeeds25RSS Feeds folder.
olFolderSuggestedContacts30Suggested Contacts folder.
olPublicFoldersAllPublicFolders18All Public Folders folder in Exchange Public Folders store.
olFolderSyncIssues20Sync Issues folder.
olFolderServerFailures22Server Failures folder (subfolder of Sync Issues folder).
olFolderConflicts19Conflicts folder (subfolder of Sync Issues folder).
olFolderLocalFailures21Local Failures folder (subfolder ofSync Issues folder).
olFolderManagedEmail29Top-level folder in Managed Folders group.

Access a Folder Opened from a Sharing Invitation

In order to access a folder received from a folder Invitation (or using Open Other User's folder command), you need find the folder on the navigation pane. This will work with default folders opened using Open Other User's Folder command or any folder shared from a sharing invitation.
Add the folder to your profile using the Open this folder button

    Dim objPane As Outlook.NavigationPane
    Dim objModule As Outlook.CalendarModule
    
    Set objPane = Application.ActiveExplorer.NavigationPane
    Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
    
    With objModule.NavigationGroups
    
        For g = 1 To .Count
        Set objGroup = .Item(g)
    
            For i = 1 To objGroup.NavigationFolders.Count
            Set objNavFolder = objGroup.NavigationFolders.Item(i)
    
' need to use the calendar name as displayed in calendar list
            If objNavFolder = "Philip Poremsky - Test" Then
             Set CalFolder = objNavFolder.Folder
            End If
            Next
        Next
    End With

More Information

OlDefaultFolders Enumeration (Outlook)

Working with VBA and non-default Outlook Folders was last modified: May 29th, 2024 by Diane Poremsky

Related Posts:

  • Move Appointments to an Archive Calendar
  • VBA: Copy New Appointments to Another Calendar
  • Save appointments to a non-default Outlook calendar folder
  • Send an Email When You Add an Appointment to Your Calendar

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
392 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

BobH (@guest_220939)
February 12, 2024 7:20 pm
#220939

As you had stated, GetFolderPath() won't work on a shared mailbox. I tried your suggestion to use the following code: Dim NS As NameSpace Dim Owner As recipient Set NS = GetNamespace("MAPI") Set Owner = NS.CreateRecipient("<display name of shared mailbox>") Set temp = NS.GetSharedDefaultFolder(Owner, olFolderInbox).Parent.folders("CURRENT PROJECTS SAMPLE FOLDER") Set Source = temp.folders("Master SAMPLE to copy 2023") 1) The 'Set temp...' statement evaluated OK and returned an object, the 'Set Source...' statement got a run time error '...object not found'. 2) Tried to only copy the 'Master SAMPLE...' folder to my primary mailbox, then use Set Source = NS.GetSharedDefaultFolder(Owner, olFolderInbox).Parent.folders("Master Sample...") and that failed with the same '...Object not found' error (also failed if copied to a PST file). It appears that this folder structure can NEVER be used, even if copied out of the shared mailbox to either a PST or a 'real' exchange mailbox. Any solution to this issue? The objective of my script is to copy this folder structure (a folder template that contains 1 top-level folder that would be renamed to the actual project name and several subfolders. This folder structure would end up getting copied to a chosen folder in the shared mailbox (utilizing .PickFolder). It… Read more »

0
0
Reply
Stratis (@guest_219579)
August 1, 2022 7:47 am
#219579

i have a VBA in Excell that updates various calendars all of them in same level as inbox,
in cell A i have the name of the calendar . However only the default calendar is updated
For example i have Calendar, Birthdays, HolidaysinGreece, DCalendar, E Calendar
Any ideas what needs to be corrected?

If xRg.Cells(i, 1) = "Birthdays" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays")
  ElseIf xRg.Cells(i, 1) = "DCalendar" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar")
  ElseIf xRg.Cells(i, 1) = "ECalendar" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar").Folders("ECalendar")
  ElseIf xRg.Cells(i, 1) = "holidaysinGreece" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar").Folders("ECalendar").Folders("holidaysinGreece")
  Else
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
  End If

   
  Set objApptItems = objCalendar.items
  Set objHoliday = objApptItems.Add

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Stratis
August 1, 2022 10:26 pm
#219584

First off, I would use the method in this article - https://www.slipstick.com/developer/create-appointments-spreadsheet-data/ - with the full folder name in the column instead of if's - change

Set subFolder = CalFolder.Folders(arrCal)
to
Set subFolder = CalFolder.Parentl.Folders(arrCal)

This identifies birthdays as a subfolder, not at the same level as the calendar/inbox etc.
Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays")

When the folder isn't found, it uses the Else statment.

0
0
Reply
Stratis (@guest_219586)
Reply to  Diane Poremsky
August 2, 2022 3:45 am
#219586

Thank you Diane,
i tried to use your method mentioned instead, Yes it works fine in the subfolders, so started customising my file but not in the main calendar, Any ideas ?

Calendars.png
change in Excel.png
Error.png
0
0
Reply
Stratis (@guest_219577)
July 31, 2022 3:11 pm
#219577

Hi, Dianne
I have in Excell in Column A different calendars
as well as shared ones, Each row is a task
I have created a VBA that automatically created
Automatically i am trying to update the calendars i note in Column N (Column 14) , However only the default calendar is updated....
Now sure what i am doing wrong
In the code attached i am trying to update 2 test calendars Dcalendar and E Calendar that are on the same level with my calendar

but instead the default calendar is only updated,

Sub AddAppointments()

  Dim objNameSpace As Namespace
  Dim objCalendar As Folder
  Dim objHoliday As AppointmentItem

  Set objNameSpace = GetNamespace("MAPI")
   
....
   
  If xRg.Cells(i, 14) = "Birthdays" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays")
  ElseIf xRg.Cells(i, 14) = "DCalendar" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar")
  ElseIf xRg.Cells(i, 14) = "ECalendar" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar").Folders("ECalendar")
  ElseIf xRg.Cells(i, 14) = "holidaysinGreece" Then
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar).Folders("Birthdays").Folders("DCalendar").Folders("ECalendar").Folders("holidaysinGreece")
  Else
  Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
  End If
   
   .....
  Set objApptItems = objCalendar.items

0
0
Reply
Todd Palermo (@guest_219487)
June 29, 2022 10:06 am
#219487

Hi Diane,

Thank you for your article. I am having some trouble with Shared Contacts. The goal is to copy all contacts from a Shared Contact folder to one of my own folders. The Shared Folder is a folder under the default folder from Example_Name@email.com. We are using Exchange. The Destination_Folder is a folder under my default folder. Are you able to tell what I am missing?

Sub CopyContacts()

Dim ol_ContactItem As Outlook.ContactItem
Dim ol_Name As Outlook.Namespace
Dim ol_Folder As Outlook.Folder
Dim ol_SharedFolder As Outlook.Folder
Dim ol_Item As Object
Dim ol_Recipient As Outlook.Recipient

Set ol_Name = Outlook.GetNamespace("MAPI")
Set ol_Recipient = ol_Name.CreateRecipient("Example_Name@email.com")
ol_Recipient.Resolve

Set ol_Folder = ol_Name.GetDefaultFolder(olFolderContacts)
Set ol_SharedFolder = ol_Name.GetSharedDefaultFolder(ol_Recipient, olFolderContacts).Folders("Source_Folder")

If ol_Recipient.Resolved Then

  For Each ol_Item In ol_SharedFolder.Items

    If ol_Item.Class = olContact Then
      Set ol_ContactItem = ol_Item.Copy
      ol_ContactItem.Move ol_Folder.Folders("Destination_Folder")
    End If

  Next

End If

End Sub

Last edited 2 years ago by Todd Palermo
0
0
Reply
Ian (@guest_218943)
December 4, 2021 6:08 pm
#218943

Can I use this to open a shared folder in a new window?

0
0
Reply
John Jakay (@guest_218783)
October 7, 2021 5:02 pm
#218783

how do I access a shared mailbox folder that is at same level as inbox?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  John Jakay
November 2, 2021 12:26 pm
#218853

use .parent -
Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
Set myfolder = newCalFolder.parent.folders("My Folder")

0
0
Reply
Fry (@guest_218674)
August 25, 2021 11:35 am
#218674

Thank you so much for this!
I'm trying to have the script read emails in shared Sent Items folders, but I'm getting the following at the Set objFolder = NS.GetSharedDefaultFolder(objOwner, olFolderSentMail) command:
"Run-time error '-2147024809 (80070057)':
Sorry, something went wrong. You may want to try again."

Could this be a permissions issue or am I syntaxing something wrong maybe?
Here's the code:

Public NS As NameSpace, objSelection As Outlook.Selection, objFolder As Folder

Sub SentByCG()

Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("Team Email")
objOwner.Resolve
If objOwner.Resolved Then Set objFolder = NS.GetSharedDefaultFolder(objOwner, olFolderSentMail)
Call ReadEm

End Sub

I tried using your UDF too but it isn't returning anything for "Items".
This is an important project so all help is greatly appreciated. Thank you!

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Fry
November 2, 2021 3:28 pm
#218855

I thought it was a permissions issue, but I'm getting it on mailboxes with the correct permissions. I don't know what the cause is (am looking into it - suspect its related to the default Exchange feature that saves sent items in the senders mailbox, not the shared mailbox) but getting the inbox or any other default folder then setting the sent items folder works -

Set olInbox = objNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
Set olSentFolder = olInbox.Parent.Folders("Sent Items")

0
0
Reply
EagleDTW (@guest_218648)
August 18, 2021 6:40 pm
#218648

Hi Diane,

Found your code most helpful and implemented a method to mark read only most recent emails within a top level folder (and not a default folder name), hope this helps someone in the future as well:

Sub Sent_Email()
 Dim Ns As Outlook.NameSpace
 Dim i As Integer
 
  Set Ns = Application.GetNamespace("MAPI")
 
'use the default folder
Set objInbox = Ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Sent Email").Items
objInbox.Sort "[ReceivedTime]", True
i = 3
'do whatever
For Each objMessage In objInbox
If objMessage.Unread = True Then
 objMessage.Unread = False
 i = 3
ElseIf i = 0 Then GoTo LastLine
Else
 i = i - 1
End If
Next
LastLine:
Set objInbox = Nothing
Set Ns = Nothing
 
End Sub

Thank you,
Daniel

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 15

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • Disable "Always ask before opening" Dialog
  • Adjusting Outlook's Zoom Setting in Email
  • This operation has been cancelled due to restrictions
  • Remove a password from an Outlook *.pst File
  • How to Hide or Delete Outlook's Default Folders
  • Outlook SecureTemp Files Folder
  • Reset the New Outlook Profile
  • Save Attachments to the Hard Drive
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
  • Use PowerShell to Delete Attachments
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Use PowerShell to Delete Attachments

Newest Code Samples

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment