I want to extract Outlook email data using Excel VBA according to specific words in subject of email.
The subject of emails changes but part of the subject is the same on all the emails.
e.g. My email Subject is "Prod - Work Daily Alert for user Steve Johnson (1234567)"
The static part of the subject is: "Prod - Work Daily Alert for user".
The dynamic part of the subject is: "Steve Johnson (1234567)".
I want to extract data from email according to the static part.
I tried to use below VBA code from StackOverflow with some modification. It does not satisfy "If" condition so it does not extract anything from email.
If I remove
If InStr(olMail.Subject, "Prod - Work Daily Alert for user") > 0 _
And InStr(olMail.ReceivedTime, x) > 0 Then
then it extracts data from all emails in the inbox.
Sub ExtractEmailContent()
Dim olApp As Outlook.Application, olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
Dim eFolder As Outlook.Folder
Dim i As Long
Dim x As Date, ws As Worksheet
Dim lRow As Long
Set ws = ActiveSheet
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
x = Date
For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
For i = olFolder.Items.Count To 1 Step -1
If TypeOf olFolder.Items(i) Is MailItem Then
Set olMail = olFolder.Items(i)
If InStr(olMail.Subject, "Prod - Work Daily Alert for user") > 0 _
And InStr(olMail.ReceivedTime, x) > 0 Then
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & lRow).Offset(1, 0).Value = olMail.Subject
.Range("A" & lRow).Offset(1, 1).Value =
olMail.ReceivedTime
.Range("A" & lRow).Offset(1, 2).Value =
olMail.SenderName
.Range("A" & lRow).Offset(1, 3).Value = olMail.CC
.Range("A" & lRow).Offset(1, 4).Value = olMail.Body
End With
End If
End If
Next i
'forward_Email ()
Set olFolder = Nothing
Next eFolder
End Sub