如何从共享 Outlook 邮箱中提取电子邮件?

How to get emails pulled in from Shared Outlook Mailbox?

我正在尝试从我们使用的共享邮箱中提取所有电子邮件,我尝试了以下方法及其工作,但仅来自我的 main/default 收件箱.

我一直在尝试通过共享邮箱使它正常工作,但似乎无法正常工作。我不是 VBA 专家,而是从其他线程中提取的,因此我们将不胜感激 :)

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)


i = 1

For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then
        Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        'Range("eMail_Recipients").Offset(i, 0).Value = OutlookMail.Recipients
        Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body

        i = i + 1
    End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub

我也试过了,但无法正常工作:

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer


Dim olShareName As Outlook.Recipient


Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set olShareName = OutlookNamespace.CreateRecipient("MailboxName")

Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("Mailbox@XYZ.com").Folders("Inbox")

您似乎需要先将 Folder 变量设置为共享收件箱,然后再搜索 olFolderInbox

这对我有用:

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Outlook.Namespace
Dim targetFolder As Outlook.MAPIFolder
Dim firstFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set firstFolder = OutlookNamespace.Folders("your shared mailbox name")
Set targetFolder = firstFolder.Folders("Inbox")

For spot = 1 To 500
    If TypeOf targetFolder.Items(spot) Is MailItem Then
        Set olMail = targetFolder.Items(spot)
        If olMail.ReceivedTime > .... Then

        ......

        End If
    End If
Next