Home > database >  Extract pdf files from an email from a specific contact (sender) and different inbox address in outl
Extract pdf files from an email from a specific contact (sender) and different inbox address in outl

Time:09-22

Good morning friends,

Please, I need your help, I have 2 problems:

1.- I wanted to be able to extract pdf files but an email from a specific contact (sender)

2.- I have several inboxes, how could I set another inbox, but not the one that comes by default - here I tried the following "Set Inbox = olNs.GetDefaultFolder ([email protected])" but it did not work for me

Thank you very much in advance

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("[email protected]")
    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 = "[email protected]" 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

CodePudding user response:

  1. It seems additionally you need to check the sender's email address of the item. The MailItem.SenderEmailAddress property returns a string that represents the email address of the sender of the Outlook item.
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 = "[email protected]" Then 
       'Set the yellow flag icon 
       obj.FlagIcon = olYellowFlagIcon 
       obj.Save 
     End If
   End If
 Next 
End Sub

However, iterating over all items in the folder is not really a good idea. Use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the following articles:

  1. Use the Store.GetDefaultFolder method instead. It returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.
  • Related