Home > Back-end >  VBA selecting right mailbox?
VBA selecting right mailbox?

Time:11-04

In my Outlook, I have two accounts configured:

  1. my main account / inbox: [email protected]
  2. a shared mailbox: [email protected]

So there are two different mailboxes.

I could hide some default folders using VBA in both mailboxes. But now I want to unhide a default folder in the shared mailbox [email protected].

This is the suggested code:

Option Explicit
  
Public Sub UnHideFolders()

Dim oFolder As Outlook.Folder
Dim oPA As Outlook.propertyAccessor
Dim PropName, Value, FolderType As String

PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B"
Value = False
 
Set oFolder = Session.GetDefaultFolder(olFolderNotes)

Set oPA = oFolder.propertyAccessor

oPA.SetProperty PropName, Value
 
Set oFolder = Nothing
Set oPA = Nothing
End Sub

This works fine for the first mailbox account / inbox [email protected], but I can't get it to work for the second account / shared mailbox [email protected].

How do I have to change the above code to unhide folders in the second account / shared mailbox?

Thanks in advance for your support!

A somehow desperate Chipy

CodePudding user response:

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("[email protected]") 
 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.

CodePudding user response:

To get the folder, try

Set oFolder = Session.Accounts("[email protected]").DeliveryStore.GetDefaultFolder(olFolderNotes)

... then re-use your existing code to manage the folder.

  • Related