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.
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)
Hi! Im having problems with NDR messages, especially moving them with macros to a specific folder in a mailbox. This folder is not in the inbox, its at the same level as the inbox. Can you help me edit this macro to move the NDRs to this folder?
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
On Error Resume Next
If UCase(Item.MessageClass) = "REPORT.IPM.NOTE.NDR" Then
Set Folders = Session.GetDefaultFolder(olFolderInbox).Folders
Set Folder = Folders.Item("nem_kezbesitheto")
If Folder Is Nothing Then
Folder = Folders.Add("nem_kezbesitheto")
End If
Item.Move Folder
End If
End Sub
If at the same level as the inbox, use
Set Folders = Session.GetDefaultFolder(olFolderInbox).parent.Folders
Hi there,
How I will get favorite Item
Entries on Favorites are not accessible, AFAIK.
I have ran a script to hide folders in outlook 365 and have acidently hidden my inbox. This had been replicated through to OWA and am unable to see my inbox folder in both the outlook app and OWA but in OWA I cam able to view the emails from the inbox but not to actual inbox folder.
Could you please advise how I can get back the inbox folder in my outlook app so it will be replicated on the server and show in my OWA.
I hope to hear form you soon
You can either use a macro to unhide it or mfcmapi
https://www.slipstick.com/outlook/delete-outlooks-default-folders/#hide
use Set oFolder = Session.GetDefaultFolder(olFolderInbox) for the folder, in place of the current folder line, and change the value from True to False then run it.