The following code can be used for getting the folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Notes folder for the user who is currently logged on:
Set oFolder = Session.GetDefaultFolder(olFolderNotes)
To get folders from a shared account you need to use the NameSpace.GetSharedDefaultFolder method which returns a Folder
object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar
folder). The following code illustrates a possible usage of the method to get a shared calendar folder:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("shared@mail.com")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
Note, the NameSpace.CreateRecipient method accepts the name of the recipient - it can be a string representing the display name, the alias, or the full SMTP email address of the recipient.
If that is not a standard folder or visible in Outlook you may consider using the Store.GetDefaultFolder method which returns a Folder
object that represents the default folder in the store and that is of the type specified by the FolderType
argument. This method is similar to the GetDefaultFolder
method of the NameSpace
object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder
returns the default folder on the default store for the current profile.