How to set Outlook sub-folders of a shared default folder in VBA?

扶醉桌前 提交于 2020-06-16 04:41:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!