如何从共享文件夹中获取电子邮件正文?

时间:2019-01-21 10:50:48

标签: excel vba outlook outlook-vba

以下代码可帮助我从收件箱中的默认文件夹中获取数据,但我 想要更改我的共享文件夹

我已经尝试用getDefaultFolder来更改sharedDefaultFolder,但这是行不通的。

Dim olApp As Object
Dim olNs As Object

Dim olFldr As Object
Dim olItms As Object
Dim olMail As Object

Set olApp = OutlookApp()
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(6).Folders("impMail")
Set olItms = olFldr.Items

2 个答案:

答案 0 :(得分:0)

您的位置“ set olFldr ...”是否为您提供了正确的文件夹?

您可以使用以下语句检查文件夹:

for each myO in olNs.GetDefaultFolder(6).folders : debug.Print myO.name : next

答案 1 :(得分:0)

您不能仅将 GetDefaultFolder 更改为 GetSharedDefaultFolder ,还必须添加收件人对象文件夹的所有者。< / em>

表达式: .GetSharedDefaultFolder(Recipient**, FolderType)

带有电子邮件地址的示例

Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Set olNs = Application.GetNamespace("MAPI")

    Dim RecipientShareName As Outlook.Recipient
    Set RecipientShareName = olNs.CreateRecipient("0m3r@email.com") 'address
        RecipientShareName.Resolve

    Dim ShareInbox As Outlook.Folder
    Set ShareInbox = olNs.GetSharedDefaultFolder(RecipientShareName, _
                                                 olFolderInbox) 'Inbox


    Dim Items As Outlook.Items
    Set Items = ShareInbox.Items

    Dim i As Long
    Dim Item As Outlook.MailItem


    For i = Items.Count To 1 Step -1

        If TypeOf Items(i) Is Outlook.MailItem Then
            Set Item = Items(i)
            Debug.Print Item.Subject '// Print Item to Immediate window
        End If

    Next

End Sub

或者如果您仅使用“姓名”,请确保 recipient object is resolved

带有收件人姓名的示例

Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Set olNs = Application.GetNamespace("MAPI")

    Dim RecipientShareName As Outlook.Recipient
    Set RecipientShareName = olNs.CreateRecipient("0m3r") 'address
        RecipientShareName.Resolve

    If Not RecipientShareName.Resolved Then
        MsgBox "Error on Recipient Object"
        Exit Sub
    Else
        Dim ShareInbox As Outlook.Folder
        Set ShareInbox = olNs.GetSharedDefaultFolder(RecipientShareName, _
                                                     olFolderInbox) 'Inbox
    End If


    Dim Items As Outlook.Items
    Set Items = ShareInbox.Items

    Dim i As Long
    Dim Item As Outlook.MailItem


    For i = Items.Count To 1 Step -1

        If TypeOf Items(i) Is Outlook.MailItem Then
            Set Item = Items(i)
            Debug.Print Item.Subject '// Print Item to Immediate window
        End If

    Next

End Sub