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
Hey Diane. I am facing a major issue with my outlook 2013. I have 3-4 shared mailbox calendars. I have also added a SharePoint 2013 calendar using "Connect to Outlook" functionality. However, I have noticed that whenever I try and create any event in the shared mailbox calendars, the SharePoint calendar automatically disappears. This happens when I double click on any date of the shared mailbox calendars or if I select a date and click on "New Appointment/Meeting" in shared mailbox calendars. Also, I have to close my Outlook and have to perform "Connect to outlook" each time from the SharePoint site whenever this happens. Do you have any solution to this? I have been stuck with this issue since last month with no possible solution. Thanks for your help.
PS: This is happening only when we try and create events in shared mailbox calendar. Tried the same for multiple users and all are facing the same issue i.e the SharePoint calendar suddenly disappears.
That is a new issue to me - sounds like an update added a bug. I'll see if i can repro it.
Thank you for reverting Diane. Please let me know if you happen to find a solution for this issue.
Hey Diane. Did you get a chance to look into this issue? Thanks for your help.
No, not yet. Will try to take a look at it tomorrow.
Hi Diane,
Thanks for this work but I can not make it work, Sorry but i have no experience with macros and I do not know what to put in Sub
I try for example:
Sub Application_ItemSend()
But always receibe a error from the Sub line
I use O2016
With this macro i want to move the sent folder from "bandeja de enviados" to "Sent" because my imap server don't suport XLIST
Thanks!
what is the full macro you are trying to use? Which line errors?
Hi Again,
I have 2 acounts and i want that default sent for one of them change to "sent" folder instead "documentos enviados"
If a put: Session.GetDefaultFolder(olFolderSentMail).Parent._Folders("Sent").Items
I have an compilation error: Character unknow.
Whitout "_" is ok, but when execute i have another error: 424
an object is required
I think that is for de "If" that i add but without lines "if.." and "End If" continue without work with error: -2147221233 (8004010f):
an object was not found
All is not KO, i'm a disaster :/
My current full code that i'm triying:
Private Sub Application_Startup()
If Item.SendUsingAccount = "acount@domain.com" Then
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
'use the default folder
Set Items = Session.GetDefaultFolder(olFolderSentMail).Parent.Folders("Sent").Items
'do whatever
End If
End Sub
Thansk a lot
>> Session.GetDefaultFolder(olFolderSentMail).Parent._Folders("Sent").Items
The _ does not belong... is there a folder named Sent at the same level as the Sent Items folder? Try using Session.GetDefaultFolder(olFolderSentMail).Items - that tells outlook to look at the default sent folder.
If that fails use olFolderInbox instead of olFolderSentMail - Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Sent").Items
One more thing:
I started doing a rule like this:
It worked, but the mail copied is not the same that the original, i think that it have a flag that say that is a copy and this mail is not sincronice, all the folder is synchronized, all mails in this folder are synchronized less the mail copied whit this rule.
when I realized I could not believe it, and i can't change it, I have tried everything that has occurred and nothing, the copie not sincronice.
Are you using an IMAP account? I've seen this behavior with some imap accounts.
hello,
Today i do two replys to Diane but i can see them now,
I don`t understand ¿they are eliminated by moderator?, ¿why?
They are here - messages are kept in moderation until i have time to answer them (it makes it easier to find the messages) and I'm a few days behind in answering messages here. :(
Hey Diane. Is there a way to copy all the existing events from one calendar to another?
Using VBA or just manually? Yes to both.
If you are doing it manually, use a list view. https://www.slipstick.com/tutorial/merging-two-calendar-folders/
I have a macro here - https://www.slipstick.com/developer/copy-new-appointments-to-another-calendar-using-vba/ that could easily be changed to copy all events.
How would I use VBA to give specific permission to those the calendar is share with?
you'll need to use Redemption to set it using VBA. http://www.dimastr.com/redemption/RDOACL.htm
Hi Diana. Can you please assist me in copying events from a calendar other than the default calendar? Currently, I have the code working for copying events from my default calendar. Thanks for all your help
Where is the folder? If its a subfolder of the default, use
Session.GetDefaultFolder(olFolderCalendar).Folders("SharedCal")
If its at the same level as the default folder, use
Session.GetDefaultFolder(olFolderCalendar).parent.Folders("SharedCal")
The folder that I want to copy the items from is a calendar that has been shared by a user. How do I modify my code so that I can copy events from this calendar? Also, will the delete functionality work if I use this calendar instead of my default calendar. Thank you for helping me out
You'll need to use the code to resolve the user. Depending on permissions, the deleted event might be in your calendar or the calendar owner's folder. If its in the owner's folder, you can watch that folder provided it was shared with you and is open in your profile.
Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
Set deletedFolder = NS.GetSharedDefaultFolder(objOwner, olFolderDeletedItems)
Also, I tried using another calendar to copy from instead of my default outlook calendar. I was able to copy the new appointments/edit appointments but when I deleted the items from the calendar from which I copied these items, the copied events in the new calendar were not getting deleted. I checked my deleted items folder and found that only the deleted items from my default outlook calendar were appearing there and the deleted items from any other calendar are not appearing in that folder. Is there a way I can make the delete functionality work?
Where is the calendar - in a shared mailbox, or a different mailbox? You'd have to watch the correct deleted items folder.
Thank you so much Diane. It worked when I passed the calendar name to Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar). Also, is it possible to move the deleted events from a SharePoint calendar to the Deleted Items folder?
don't the SharePoint events go into your own deleted items folder? if you mean, you need to watch to folders, you can - you need to add a new folder to watch and duplicate the code that deletes.
Hi Diana I'm working on a script to automatically create calendar appointments based on the subject line in an email which I have working great for my default calendar. The subject line format is as follows: "new appointment, appointment subject, location, start date & time 1/1/2016 4 PM, duration in minutes" I have multiple different calendars and one of the strings in the subject line will determine which calendar the appointment will be saved to. The calendar name will be in "apptArray(2)" For example, one of the calendars is called "Conf 1" so my email subject line would be: "new appointment, Richard, Conf 1, 30/04/18 12:30 p, 60" How would I modify this code below to choose which calendar I want the appointment to be created in? I would really appreciate your assistance? ---------------------------------------------------------------------------------- Dim WithEvents olInbox As Items Private Sub Application_Startup() Dim ns As Outlook.NameSpace Set ns = Application.GetNamespace("MAPI") Set olInbox = ns.GetDefaultFolder(olFolderInbox).Items Set ns = Nothing End Sub Private Sub olInbox_ItemAdd(ByVal Item As Object) ' subject is arranged like this: ' new appointment, appointment subject, location, start date & time 1/1/2016 4 PM, duration in minutes ' do not use commas except as separators If InStr(1, LCase(Item.Subject), "new… Read more »
>> Set objAppt = Application.CreateItem(olAppointmentItem)
uses the default folder.
You need to set the folder - which will easiest if the folders are all within the same mailbox at the same level (or all are the Calendar in shared mailboxes). For example, all are subfolders of the default calendar.
Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
arrCal = apptArray(2)
Set subFolder = CalFolder.Folders(arrCal)
Set objAppt = subFolder.Items.Add(olAppointmentItem)
Thanks for your help! I'm a rookie at this so I'm struggling a bit with the syntax
Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
I keep getting the same error on the above line:
Run-Time Error '424': Object Required
Also, all my calendars are on the same level as my default calendar, they are all under the root directory, how would that change the syntax of the code you recommended?
This would get a folder at the same level as the default calendar"
olNs.GetDefaultFolder(olFolderCalendar).Parent.Folders("Conf1")
I'll need to see the full code to know why the error - best guess in the namespace object, olns, isn't set.
Hi Diana,
Thanks for the tutorial.
I am working on a utility where in I need to access a shared calendar of other person and update its appointments. The person has shared the calendar with me with "Owner" access right and also I can see the calendar under my "Shared Calendars". However, when I access or display the calendar I get an error- "Cannot display the folder. Microsoft Outlook cannot access the specified folder location."
Below is my code. Appreciate any help!!
Set Application = GetObject(, "Outlook.Application")
If Application Is Nothing Then Set olApp = CreateObject("Outlook.Application")
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("xyz")
myRecipient.Resolve
If myRecipient.Resolved Then
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
do you get that message all the time when you try viewing it or only when you use the macro? It's a weird error for the situation - it typically means something is wrong with the ost. Renaming the ost should fix the error.
Hi , thanks for tutorial.
can you share how to include all folders inside a default mailbox. I have folders inside inbox and subfolders inside folders.
Does the below code include all items under sharedmailbox folder inbox
that would only work on the folder named "folder_under_folder2". If you need to work with each subfolder, you need to "walk" the folders, using code like this: http://www.vboffice.net/en/developers/looping-recursively-through-folders-and-subfolders