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 SubTo 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").ItemsWhen the folder is a subfolder under the default Calendar folder, use this instead:
Set Items = Session.GetDefaultFolder(olFolderCalendar)._
Folders("SharedCal").ItemsTo 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
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.
| Name | Value | Description |
|---|---|---|
| olFolderCalendar | 9 | Calendar folder. |
| olFolderContacts | 10 | Contacts folder. |
| olFolderDeletedItems | 3 | Deleted Items folder. |
| olFolderDrafts | 16 | Drafts folder. |
| olFolderInbox | 6 | Inbox folder. |
| olFolderJournal | 11 | Journal folder. |
| olFolderJunk | 23 | Junk E-Mail folder. |
| olFolderNotes | 12 | Notes folder. |
| olFolderOutbox | 4 | Outbox folder. |
| olFolderSentMail | 5 | Sent Mail folder. |
| olFolderTasks | 13 | Tasks folder. |
| olFolderToDo | 28 | To Do folder. |
| olFolderRssFeeds | 25 | RSS Feeds folder. |
| olFolderSuggestedContacts | 30 | Suggested Contacts folder. |
| olPublicFoldersAllPublicFolders | 18 | All Public Folders folder in Exchange Public Folders store. |
| olFolderSyncIssues | 20 | Sync Issues folder. |
| olFolderServerFailures | 22 | Server Failures folder (subfolder of Sync Issues folder). |
| olFolderConflicts | 19 | Conflicts folder (subfolder of Sync Issues folder). |
| olFolderLocalFailures | 21 | Local Failures folder (subfolder ofSync Issues folder). |
| olFolderManagedEmail | 29 | Top-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.

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
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 »
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
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.
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 ?
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
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 SubCan I use this to open a shared folder in a new window?
how do I access a shared mailbox folder that is at same level as inbox?
use .parent -
Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
Set myfolder = newCalFolder.parent.folders("My Folder")
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 SubI 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!
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")
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