问题
I am trying to extract information from a shared Outlook inbox. The problem occurs when I try to set the Outlook Folder to a sub-folder of the shared olFolderInbox. The strange part is that some days the Macro will run perfectly and extracts the data I need. However, sometimes the Macro will fail to find the Folder object and displays an error message even though the code was untouched.
When I set the folder to the main inbox (olFolderInbox) the code works perfectly, however, when I try to set it to a specific sub-folder, the code will sometimes fail. I ensured that the sub-folder name was correct.
Here is my code:
Dim OutlookNameSpace As Outlook.Namespace
Dim OutlookFolder As Outlook.Folder
Dim OutlookMail As Outlook.MailItem
Dim ObjOwner As Outlook.Recipient
Set OutlookNameSpace = Outlook.Application.GetNamespace("MAPI")
Set ObjOwner = OutlookNameSpace.CreateRecipient("EmailName@email.ca")
ObjOwner.Resolve
If ObjOwner.Resolved Then
Set OutlookFolder = OutlookNameSpace.GetSharedDefaultFolder _
(ObjOwner, olFolderInbox).Folders("FolderName")
End If
The error message I receive is:
"Run-time error '-2147221233 (8004010f)':
The attempted operation failed. An object could not be found."
The debugging tool highlights the line where I tried to set the sub-folder as the Outlook Folder.
When I comment out: .Folders("FolderName") the Macro runs fine.
If you could please tell me why this code sometimes works, but other times fails, it would help me out a lot!
Thank you!
回答1:
Keep in mind that if the delegate default folders are cached (in the primary mailbox's OST file), the sub folders will not be present.
Try to turn caching off for the delegate folders - uncheck the "Download shared folders" checkbox on the Advanced tab of the Exchange account properties dialog.
来源:https://stackoverflow.com/questions/56938788/how-to-set-outlook-sub-folders-of-a-shared-default-folder-in-vba