Code to read Outlook mail with specific subject

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have this working code which reads the mail and its attachment into a local folder. It is working well.
But a new requirement is to read the mails with subject containing the words, Customer ID 1000 - Smith, JOHN P, where the word Customer ID is always static but the number and name is dynamic.
I am not sure how to resolve this as my modified code does not read the mail which contains the word, Customer ID.

Code:
Private Sub cmdOutlook_Click()
Dim olApp As Object
Dim MYFOLDER As Object
Dim OlItems As Object
Dim olMail As Object
Dim x As Integer
Dim subject As String
Dim strFile As String
Dim strFolderpath As String
Dim objDestfolder As Object
Dim filterKeywords As String
Dim filter As String


Set olApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
    Set olApp = CreateObject("Outlook.Application")
End If

strFolderpath = "C:\Users\Testing"
'On Error Resume Next

' Set the Attachment folder.
strFolderpath = strFolderpath & "\Attachments\"

Set MYFOLDER = olApp.GetNamespace("MAPI").Folders("Customer Mailbox").Folders("Inbox")


 Set OlItems = MYFOLDER.Items

  'Working code
  'For Each OlMail In OlItems
        'strFile = OlMail & ".XML"
        'strFile = strFolderpath & strFile
        'If OlMail.Attachments.Count > 0 Then
            'For x = 1 To OlMail.Attachments.Count
                'OlMail.Attachments.item(x).SaveAsFile strFile
            'Next x
        'End If
    'Next

'Modified code
 For i = OlItems.Items.Count To 1 Step -1
        If TypeOf OlItems.Items(i) Is MailItem Then
            Set olMail = OlItems.Items(i)
            If InStr(olMail.subject, "Customer ID") > 0 Then ' how to use the Like condition here?
                strFile = strFolderpath & strFile
                If olMail.Attachments.Count > 0 Then
                    For x = 1 To olMail.Attachments.Count
                            olMail.Attachments.Item(x).SaveAsFile strFile
                    Next x
                End If
        End if
    End if
Next




Set MYFOLDER = Nothing
Set olMail = Nothing
Set OlItems = Nothing
Set olApp = Nothing
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Not sure I understand what the problem is.
my modified code does not read the mail which contains the word, Customer ID.
This should return a number higher than zero if "Customer ID" is in the subject.
If InStr(olMail.subject, "Customer ID") > 0 Then
Since I don't have the emails to work with I'll show it this way: The value returned by
instr("Customer ID 1000 - Smith, JOHN P,", "Customer ID") is 1. There is no way to use LIKE operator within Instr function and it's not necessary. If the function returns 0 for every email, then there is something wrong with where or how you're searching. Try a Debug.Print line before the If block that tests for Instr then examine the list of subjects. If you have hundreds of emails in the loop you might want to change this - OlItems.Items.Count to a smaller number at first.
 
Upvote 0
Not sure I understand what the problem is.

This should return a number higher than zero if "Customer ID" is in the subject.

Since I don't have the emails to work with I'll show it this way: The value returned by
instr("Customer ID 1000 - Smith, JOHN P,", "Customer ID") is 1. There is no way to use LIKE operator within Instr function and it's not necessary. If the function returns 0 for every email, then there is something wrong with where or how you're searching. Try a Debug.Print line before the If block that tests for Instr then examine the list of subjects. If you have hundreds of emails in the loop you might want to change this - OlItems.Items.Count to a smaller number at first.
What I am looking for is, the code should recognize mails with subject which has the wording, Customer ID. It then goes through the mail item and saves the attachment in the local folder.
As I mentioned, the word Customer ID is constant whereas the the number and customer names change.
what about,
If olMail.Subject LIKE "*Customer ID*" Then
will that work?
I am away from my work computer and haven't tried yet.
 
Upvote 0
the code should recognize mails with subject which has the wording, Customer ID
Sorry, I do not see what else I can add to this beyond what I posted. What I showed would do exactly that IF in fact it is true.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top