简体   繁体   中英

Connection to subfolder of the inbox of a shared mailbox

On my laptop it works, however for the team the code works only sometimes. They get below error message.

It is supposed to connect to a specific Outlook folder.

When I reduce code to

Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '//  works fine

then there is no issue on their side, however when variable is extended to specific folder it works 1 out of 10 times.

References are set in Excel, folders names are correct because it works on my laptop.

在此处输入图片说明

in line在此处输入图片说明 `

Sub macro()

    'declare variable
     Dim olApp As Outlook.Application
     Dim Ns As Outlook.Namespace
     Dim Folder As Outlook.MAPIFolder
     Dim olShareName As Outlook.Recipient
     Dim olMailItem As Outlook.MailItem

    'clear objects
     Set olApp = Nothing
     Set Ns = Nothing
     Set olShareName = Nothing

    'set outlook variable
     Set olApp = New Outlook.Application
     Set Ns = olApp.GetNamespace("MAPI")
     Set olShareName = Ns.CreateRecipient("xxx@xx.com") /// Owner's email address
    
   
     Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("SHAREPOINT COO").Folders("COO") '//  doesn't work
     'Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '//  works fine
     Set Items = Folder.Items
    
end sub

The error means the folder does not exist. Keep in mind that Namespace.Folders returns the top level folders of all stores in the profile. Unless you have a mailbox (not a folder) named "Inbox", that line will fail.

If you want the Inbox folder, use Namespace.GetDEfaultFolder(olFolderInbox) instead.

Use the Recipient.Resolve method which attempts to resolve a Recipient object against the Address Book before calling the NameSpace.GetSharedDefaultFolder method.

Set olShareName = Ns.CreateRecipient("xxx@xx.com") /// Owner's email address

olShareName.Resolve 

Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox)

Also I'd recommend breaking the chain of property or method calls by declaring each property or method call on a separate line of code. Thus, you will be able to find out which property or method fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM