EXCEL VBA code to retrieve sharedmailbox all mails from root folders to subfolders

旧巷老猫 提交于 2019-12-18 09:44:51

问题


I have a code currently which I think should pull all mail items specified from only default folder.

It's not working as I expected. I know there is some problem in looping all folders in shared mail box from root. How can fix it?

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Outlook.Namespace
Dim olShareName As Outlook.Recipient
Dim Folder As MAPIFolder
Dim eFolder As Outlook.Folder
Dim olItems As Outlook.Items
Dim OutlookMail As Variant
Dim arrResults() As Variant
Dim ItemCount As Long

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set olShareName = OutlookNamespace.CreateRecipient("example@example.com")

For Each eFolder In OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders

    Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders(eFolder.Name)

    Set olItems = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("From_date").Value & "' and [ReceivedTime] <= '" & Range("to_date").Value & "'")

    If olItems.Count > 0 Then
        ReDim arrResults(1 To olItems.Count, 1 To 5)
        ItemCount = 0
        For Each OutlookMail In olItems
            ItemCount = ItemCount + 1
            arrResults(ItemCount, 1) = OutlookMail.Subject
            arrResults(ItemCount, 2) = OutlookMail.ReceivedTime
            arrResults(ItemCount, 3) = OutlookMail.SenderName
            arrResults(ItemCount, 4) = OutlookMail.Size
            arrResults(ItemCount, 5) = OutlookMail.Categories
        Next OutlookMail
        Worksheets("import").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults
    Else
        MsgBox "No items found!", vbExclamation
    End If

    Set olItems = Nothing

    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set olShareName = Nothing
    Set OutlookApp = Nothing
Next eFolder

回答1:


Keep in mind that by default Outlook caches shared folders in the primary mailbox's OST file. Subfolders are not cached. Try to disable the shared folder caching in the Exchange account properties dialog.



来源:https://stackoverflow.com/questions/49597851/excel-vba-code-to-retrieve-sharedmailbox-all-mails-from-root-folders-to-subfolde

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