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

3
  • The error indicates the subfolder is not directly under the inbox. Add more .Folders To include all folders between the inbox and Example_Subfolder. Commented Mar 9, 2022 at 18:25
  • I'm sure the "Example_Subfolder" is in the inbox folder. By printing out subFolder.FolderPath, I get "\\[email protected]\Inbox\Example_Subfolder". (I get this result by first using the Set subFolder - olNS.PickerFolder code. Commented Mar 11, 2022 at 0:49
  • Does this answer your question? How to set Outlook sub-folders of a shared default folder? Commented Mar 11, 2022 at 12:40

1 Answer 1

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("[email protected]")
Set olInbox = olMailbox.Folders("Inbox")
Set subFolder = olInbox.Folders("Example_Subfolder")
subFolder.Display

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Hi niton, apologies for the delayed response. I've run your code and I get an error on olMailbox = olNS.Folders("[email protected]").
In case there is a typo right click on the mailbox and copy the name from Data File Properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.