0

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.

enter image description here

in line enter image description here `

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
  • Does this answer your question? [How to set Outlook sub-folders of a shared default folder?](https://stackoverflow.com/questions/56938788/how-to-set-outlook-sub-folders-of-a-shared-default-folder) – niton Jul 16 '21 at 19:54

2 Answers2

0

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I see You did not read carefully what I have written. On my laptop code works fine, folder exists, all of them. If i'm connecting only to Inbox folder it works on every one laptops. However if I try to connect to that path Inbox -> Sharepoint COO -> COO then error occurs on other laptops ( on my it works fine, folders exists) – Łukas Ptak Jul 16 '21 at 19:05
  • Then the mailbox with that name ("Inbox") does not exist in the user's active profile. Did you make sure there is a root level folder (mailbox) with that name? – Dmitry Streblechenko Jul 16 '21 at 20:14
  • As I mentioned before - on my laptop code works every time, for others it works 1 out of 10 times. – Łukas Ptak Jul 19 '21 at 06:01
  • Once again, on the problematic machine, did you make sure there is a root level folder (mailbox) with that name? – Dmitry Streblechenko Jul 19 '21 at 06:21
  • yes, there is. Folder inbox is available the same as sub folders. As I said - when I'm trying to connect to only Inbox it works fine on every machine, but when I trying to reach sub folder in Inbox folder then it works on my machine all the time, on problematic ones sometimes – Łukas Ptak Jul 20 '21 at 08:51
0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45