VBA - Trying to add another "and" to an "IF" and failing, miserably !!

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Trying to figure this out and not getting very far.
The snippet of code below works and it is this bit I am trying to adapt to only look at the email if it is unread.

In summary I have two Const statements setting the email address and subject parameters. When the code is run it will look in Outlook, in a specific folder - for emails from a specific sender with a specific subject line.
Within the If statement it does a few things and it sets the email to "read"

VBA Code:
    If ((StrComp(EmailSenderEmailAddress, xxxxxxEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(xxxxxxEmailSubjectKey), vbTextCompare)) Then

I would like to to include a line so that the code above will skip an email if it is already "read" and only work on unread emails.

Tried adding

VBA Code:
 and OutlookMail.unread = True

so it looks like

VBA Code:
 If ((StrComp(EmailSenderEmailAddress, VeritasEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(VeritasEmailSubjectKey), vbTextCompare)) And OutlookMail.UnRead = True Then

It doesn't throw an error when run but it must return as false (even though there are unread emails in the folder) as the IF just ends

To re-iterate, the code works if it just looks at the Outlook folder and does the compare in the code - I'd like it to only compare on unread emails

Please shout if you need all the code - I was in a bit of a rush so did some snipping :)

Thanks in advance for any help

Netrix
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I couldn't prove it.
But do it separately:
VBA Code:
If ((StrComp(EmailSenderEmailAddress, xxxxxxEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(xxxxxxEmailSubjectKey), vbTextCompare)) Then
  If OutlookMail.unread = True Then
    'do something
  End If
End If

Or vice versa:
VBA Code:
If OutlookMail.unread = True Then
  If ((StrComp(EmailSenderEmailAddress, xxxxxxEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(xxxxxxEmailSubjectKey), vbTextCompare)) Then
     'do something
  End If
End If


Regards
Dante Amor
:)
 
Upvote 0
Solution
I couldn't prove it.
But do it separately:
VBA Code:
If ((StrComp(EmailSenderEmailAddress, xxxxxxEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(xxxxxxEmailSubjectKey), vbTextCompare)) Then
  If OutlookMail.unread = True Then
    'do something
  End If
End If

Or vice versa:
VBA Code:
If OutlookMail.unread = True Then
  If ((StrComp(EmailSenderEmailAddress, xxxxxxEmailSenderAddress, vbTextCompare) = 0) And InStr(1, EmailSubject, LCase(xxxxxxEmailSubjectKey), vbTextCompare)) Then
     'do something
  End If
End If


Regards
Dante Amor
:)

Fantastic !
Thank You Dante Amor - slowly getting the hang of this stuff !!
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,360
Members
449,155
Latest member
ravioli44

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