0

I am having an issue with accessing a subfolder from a shared Outlook email box using VBA. The goal of this code is to download attachments from emails located in a subfolder called "Example_Subfolder". The code below results in an error message; "Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found.".

Sub foo()

Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim destFolder As Outlook.MAPIFolder
Dim srcFolder As Outlook.MAPIFolder
Dim olItem As Object
Dim subFolder As Object
Dim mailitem As Outlook.mailitem
Dim olAtt As Outlook.Attachment
Dim objOwner As Outlook.Recipient


Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

'set object to shared email inbox
Set objOwner = olNS.CreateRecipient("Shared_Mailbox@companyname.com")
objOwner.Resolve

'check object resolved
If Not objOwner.Resolved Then
    Debug.Print objOwner.Name
    MsgBox "Failed to connect to shared email. Contact XXX."
End If


Set olFolder = olNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
'error on next line.
Set subFolder = olFolder.Folders("Example_Subfolder")


'download email attachments
'etc
'etc
End Sub

The only way I've been able to access the emails inside "Example_Subfolder" is by using Set subFolder = olNS.PickFolder. I would rather not use this method in my macro. Can anyone point me in the right direction as to why my code doesn't work?

Qstein
  • 11
  • 4
  • The error indicates the subfolder is not directly under the inbox. Add more `.Folders` To include all folders between the inbox and Example_Subfolder. – niton Mar 09 '22 at 18:25
  • I'm sure the "Example_Subfolder" is in the inbox folder. By printing out `subFolder.FolderPath`, I get "\\Shared_Mailbox@companyname.com\Inbox\Example_Subfolder". (I get this result by first using the `Set subFolder - olNS.PickerFolder` code. – Qstein Mar 11 '22 at 00:49
  • 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 Mar 11 '22 at 12:40

1 Answers1

0

Given the folder is visible in the navigation pane there is an alternative.

Sub foo()

Dim olNS As namespace
Dim olMailbox As Folder
Dim olInbox As Folder
Dim subFolder As Folder

Set olNS = GetNamespace("MAPI")

' If the folder is in the navigation pane
Set olMailbox = olNS.Folders("Shared_Mailbox@companyname.com")
Set olInbox = olMailbox.Folders("Inbox")
Set subFolder = olInbox.Folders("Example_Subfolder")
subFolder.Display

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • Hi niton, apologies for the delayed response. I've run your code and I get an error on ```olMailbox = olNS.Folders("Shared_Mailbox@companyname.com")```. – Qstein Apr 08 '22 at 21:28
  • In case there is a typo right click on the mailbox and copy the name from Data File Properties. – niton Apr 08 '22 at 22:16