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 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.
Hi Diana
Kindly help me out.
While I paste data from excel to outlook. It is giving me a error 91 every time. Ones I press debug and press f8 then it give me a result.
So how I can fix it.
Does it say object variable not set? What is the code you are using?
Hi Diane,
What i'm trying to achieve is to check one mailbox for pdf attachments (did it this way:
Sub MoveDocFilesToCRiess(Item As Outlook.MailItem)
Dim olkAtt As Outlook.Attachment
'Check each attachment
For Each olkAtt In Item.Attachments
'If the attachment's file name ends with .pdf
If Right(LCase(olkAtt.FileName), 4) = ".pdf" Then
'Move the message to Test
Item.Move Session.GetDefaultFolder(6).Parent.Folders("test")
'No need to check any of this message's remaining attachments
Exit For
End If
Next
Set olkAtt = Nothing
End Sub)
and if found forward that mail to a different email address and if no pdf attachments found move it to another mailbox folder that's opened in the same session
Do you have a rule set to move? The code looks good, so it should be the one moving it.
Hi Diane,
You helped me write my VBA code for pulling out specific fields beautifully and I am successfully pulling in everything I want from my shared mailbox inbox. I am trying to connect to a specific folder called "TestRNCJ", and nothing has worked for me. If I try changing the "olFolderInbox" to any other function, I get an error, let alone trying to connect a specific folder to my code. Please let me know if you have any idea what's going on and thanks in advance, Diane!
Stella
You need to set the parent folder - then work your way down to the subfolder.
Set newInboxFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
Set newInboxFolder = newInboxFolder.folders("subfoldername")
Hi, I have been using the Function GetFolderPath for sometime now and it worked well - until I updated to Excel for Office 365 (32 bit).
Now the code has returns an error 438 at this line:
Set oFolder = Application.Session.Folders.item(FoldersArray(0))
I am wondering if this has to do with my references library? I have attached a snapshot of my references
Thanks for any pointers
You have the correct version Excel 16. (Office 2016, 2019, 365 are all v. 16.0)
Thank you, Diane, for the above code. I've been programming in vba for a long time and really appreciate your generosity. However, I'm afraid I may be missing something here.
Your example is intended to share contacts, yes? But all the objects in your code are Calendar objects. Can I simply replace "Calendar" where it exists with "Contacts"?
Yes, you can swap the names out -
Hello Diane,
I am new to scripting & VBA and so far I have perused the innerweb's and have come across your website. Great article and resources. This is greatly appreciated by us noobs!
No to the nitty-gritty. I am looking for a basic VBA for Outlook that will allow me to view the current sent received items of selected folders in Outlook & export to excel.
Currently I have a script that will give me a total count on selected folder, but I have to run it one folder at a time and with a screen pop that tells me the count. However, I would like to Run the VBA script that will allow users to select a folder or set of folders with possible date and time selections to get the count of sent a received emails to export to Excel.
Is this possible and would you be willing to lend a helping hand and modify my existing code?
Thank you,
DoM
It is possible. Do you want them to select random folders or a parent and it's subfolders? Parent/subs is easy - getting a list of random folders is more difficult.
Sending to excel (or a text file) is fairly easy - this is one example of how to find the next empty row in Excel. (You'll want to remove the block that writes the emails to Excel.)
Hello Diane,
I would like for them to select parent/subs. If I shared the code I currently run in Outlook, could you help modify to work with parent/subs and export to csv or excel?
Thank you,
DoM
You need to use the GetFolder function to walk the subfolders. Yes, if you share the code i can edit it to work with the function.
Diane,
Thank for your willingness to assist me with this. I have attached the code that I currently have running as a Macro in outlook.
Modify accordingly.
Thank you,
DoM
This writes just the count to a text file.
I have a similar macro at - https://www.slipstick.com/developer/print-list-of-outlook-folders/ - it splits the counts by folder.
Diane,
That worked as well as the referenced macro. Which line should I change to save to TXT, instead of opening Outlook, kind of like the fixed macro you provided me.
Thank you for your assistance you are making me look like such a hero! I am happy I came across your site!!!
That macro should save as a text file as written.
Hi Diane. Thank you for this helpful introduction to Microsoft Outlook VBA. I am currently trying to figure out a way to have an Outlook meeting 'close' after getting the meeting accepted by required roles. The meeting invite is sent out to the whole department and can only occur once the necessary people are able to attend. Would you happen to have something like this already written or have an idea of how to execute this? Thanks.
You'd need to check the acceptances and see if all accepted - its certainly doable using VBA.
I have a sample that closes a room when the room is full - that can be the basis for your macro or you can watch for declines.
https://www.slipstick.com/developer/code-samples/close-meeting-room-full/