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

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 |
olFolderSuggestedContacts | 30 | Suggested Contacts folder |
olFolderTasks | 13 | Tasks folder |
olFolderToDo | 28 | To Do folder |
olPublicFoldersAllPublicFolders | 18 | All Public Folders folder in Exchange Public Folders store (Exchange only) |
olFolderRssFeeds | 25 | RSS Feeds folder |
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
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?
Can 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:
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!
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
hi all,
any idea if there is a script to read emails automatically and update the calendar. for example a staff wants a day off, and request leave from xx/xx/xx date to xx/xx/xx. can VBA be used to read this data and update the calendar automatically.
Yes, as long as it knows what to look for. I have two examples in this article -
Create Appointment From Email Automatically (slipstick.com)