I am trying to create a code where from a certain inputs in excel (Date, subject, email body, mailbox, folder navigation and export to) I get the attachments from those certain emails downloaded.
My problem is that even though the code gets the correct mailbox and folder, and downloads attachements to the folder I want, it doesn't get the date, subject and email body. The whole objective is to download attachments from emails with the date onwards, the email subject cointains some words and the email body contains certain words, however what I am getting is the attachments from all emails on the mailbox.
Here is my code (I tried to change the & for AND but with that it doesn't even download):
Sub download_attachment()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olItem As Object
Dim mailitem As Outlook.mailitem
Dim olAtt As Outlook.Attachment
Dim Folder_Navigation As String
Dim folders() As String
Dim folderIndx As Long
Dim dateFormat
dateFormat = Format(Now, "dd.mm.yyyy")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.folders([Sheet1].[Mailbox_Name].Text)
Folder_Navigation = [Sheet1].[Folder_Navigation].Value
folders = Split(Folder_Navigation, ";")
For folderIndx = LBound(folders) To UBound(folders)
Debug.Print folders(folderIndx)
Set olFolder = olFolder.folders(folders(folderIndx))
Next folderIndx
For Each olItem In olFolder.Items
If olItem.Class = olMail Then
Set mailitem = olItem
Debug.Print mailitem.Subject
Debug.Print mailitem.ReceivedTime
If mailitem.ReceivedTime > [Sheet1].[Date].Value & _
InStr(mailitem.Subject, [Sheet1].[Subject].Value) <> 0 & _
InStr(mailitem.Body, [Sheet1].[Email_Body].Value) <> 0 Then
For Each olAtt In mailitem.Attachments
olAtt.SaveAsFile [Sheet1].[Export_To].Text & "\" & olAtt.Filename
Next olAtt
End If
End If
Next olItem
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
```
First of all, there is no need to iterate over all items in the folder as shown in your code:
For Each olItem In olFolder.Items
If olItem.Class = olMail Then
Instead, the Outlook object model provides the Find
/ FindNext
or Restrict
methods of the Items
class. So, you will be able to iterate over items that correspond to your search criteria only. Read More about these methods in the following articles:
For example, the following search criteria can be used for getting items with attachments and subject containing a keyword(s):
Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & " Like '%keyword%' AND " & _
Chr(34) & "urn:schemas:httpmail:hasattachment" & _
Chr(34) & "=1"
For more samples take a look at the VBA Outlook: Find specific attachment and save under different name and Check for the senderEmailAddress .
Also you may find the AdvancedSearch
method of the Application
class helpful. See Advanced search in Outlook programmatically: C#, VB.NET for more information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.