从特定联系人(发件人)和 outlook 中不同收件箱地址的电子邮件中提取 pdf 文件

Extract pdf files from an email from a specific contact (sender) and different inbox address in outlook

朋友们早上好,

拜托,我需要你的帮助,我有两个问题:

1.- 我希望能够提取 pdf 文件,但是来自特定联系人(发件人)的电子邮件

2.- 我有几个收件箱,我怎么能设置另一个收件箱,而不是默认的收件箱 - 在这里我尝试了以下“Set Inbox = olNs.GetDefaultFolder (onothermail@gmail.com)" 但它对我不起作用

非常感谢您

Option Explicit
Public Sub Example()
   '// Declare your Variables
    Dim olNs As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim Item As Outlook.MailItem
    Dim Atmt As Attachment
    Dim Filter As String
    Dim FilePath As String
    Dim AtmtName As String
    Dim i As Long
    Dim objOwner As Outlook.Recipient

   '// Set Inbox Reference
    Set olNs = Application.GetNamespace("MAPI")
    Set objOwner = olNs.CreateRecipient("secondMail@gmail.com")
    Set Inbox = olNs.GetSharedDefaultFolder(objOwner)
   
    
    

    FilePath = "C:\Users\Unity\Desktop\adjuntos\"
    
    Filter = "[Unread] = True"
    Set Items = Inbox.Items.Restrict(Filter)

   '// Loop through backwards
    For i = Items.Count To 1 Step -1
        Set Item = Items(i)

        DoEvents

        If Item.Class = olMail Then
            If Item.SenderEmailAddress = "senderx@gmail.com" Then

                For Each Atmt In Item.Attachments
                    AtmtName = FilePath & Atmt.FileName
                    If ((InStr(Atmt.DisplayName, ".jpg") Or InStr(Atmt.DisplayName, ".zip") Or InStr(Atmt.DisplayName, ".PDF") Or InStr(Atmt.DisplayName, ".pdf"))) Then

                        Atmt.SaveAsFile FilePath & "\" & Atmt.DisplayName
              
                    End If
                    Item.UnRead = False
                Next
            End If
            
        End If
    Next

    Set Inbox = Nothing
    Set Items = Nothing
    Set Item = Nothing
    Set Atmt = Nothing
    Set olNs = Nothing
End Sub
  1. 您似乎还需要检查该项目的发件人电子邮件地址。 MailItem.SenderEmailAddress 属性 returns 表示 Outlook 项目发件人电子邮件地址的字符串。
Sub SetFlagIcon() 
 Dim mpfInbox As Outlook.Folder 
 Dim obj As Outlook.MailItem 
 Dim i As Integer 
 Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test") 
 ' Loop all items in the Inbox\Test Folder 
 For i = 1 To mpfInbox.Items.Count 
   If mpfInbox.Items(i).Class = olMail Then 
     Set obj = mpfInbox.Items.Item(i) 
     If obj.SenderEmailAddress = "someone@example.com" Then 
       'Set the yellow flag icon 
       obj.FlagIcon = olYellowFlagIcon 
       obj.Save 
     End If
   End If
 Next 
End Sub

但是,遍历文件夹中的所有项目并不是一个好主意。使用 Items class 的 Find/FindNextRestrict 方法。在以下文章中阅读有关这些方法的更多信息:

  1. 请改用 Store.GetDefaultFolder 方法。它 returns 一个 Folder 对象,代表商店中的默认文件夹,并且是 FolderType 参数指定的类型。此方法类似于 NameSpace 对象的 GetDefaultFolder 方法。不同之处在于此方法获取与帐户关联的交付商店上的默认文件夹,而 NameSpace.GetDefaultFolder returns 当前配置文件的默认商店上的默认文件夹。