Identify specific email from Excel (by sender, date sent, and subject line)

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
If I need to create a loop that looks thru each item in my Outlook Sent folder and then evaluates it based on who the sender was and whether or not it's the original email (defined by no "RE:" or "FW:" in subject line and it was sent today), how would I go about this??

I tried something like this, but it fails (Error: RTE 287: Application-defined or object-defined error). I didn't include the date test in my example, so if you have some suggestions, I'd be thrilled.

Code:
Dim objOutlook As New Outlook.Application  ' Early binding
Dim OLNameSpace As Outlook.Namespace
  Set OLNameSpace = Outlook.GetNamespace("MAPI")
Dim OLFolder As Outlook.Folder, _
    OLFolder_Sent As Outlook.Folder
  Set OLFolder = OLNameSpace.GetDefaultFolder(olFolderInbox)
  Set OLFolder_Sent = OLNameSpace.GetDefaultFolder(olFolderSentMail)
Dim OLMail As Outlook.MailItem, _
    OLMail_items As Outlook.MailItem

For Each OLMail_items In OLFolder_Sent.Items
If InStr(OLMail_items.Subject, "RE:") = 0 And _
         InStr(OLMail_items.Subject, "FW:") = 0 And _
         [COLOR=#0000ff][B]OLMail_items.SenderEmailAddress[/B][/COLOR] = "[EMAIL="Dr_Demento@aol.com"]Dr_Demento@CompuServe.com[/EMAIL]" then

[I]do stuff
[/I]
Exit For
End If
Next OLMail_items

I realize this isn't a full loop, I just need some help with the bolded section. I get the feeling my issue is d/t which variable types I'm using but I'm too inexperienced to know what I should use.

Thanks y'all!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Your code seems to work fine. The only problem I see is that you would get a type mismatch error if your folder contains items other than mail items, such as meeting items and appointment items. So your error must be caused by some other line in your code.
 
Upvote 0
Thanks Domenic. I thought the Sent folder would only have email items. I'll clear out my folder tomorrow and give it a try.

Barring that, any suggestions on adapting the code to allow multiple item types?

Also, which property should I use to test when it was sent?

Thanks much.
 
Upvote 0
There's no need to clear out your folder. Simply declare your For Each control variable as a Variant, and test for a mail item using TypeName...

Code:
    Dim olApp As New Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olFolder As Outlook.Folder
    Dim vItem As Variant
    
    Set olNS = olApp.GetNamespace("MAPI")
    Set olFolder = olNS.GetDefaultFolder(olFolderSentMail)
    
    For Each vItem In olFolder.Items
        If TypeName(vItem) = "MailItem" Then
            If vItem.SenderEmailAddress = "Dr_Demento@CompuServe.com" Then
                If InStr(vItem.Subject, "RE:") = 0 And InStr(vItem.Subject, "FW:") = 0 Then
                    'etc
                    '
                    '
                End If
            End If
        End If
    Next vItem

Actually, here's another way, which should be more efficient...

Code:
    Dim olApp As New Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim vItem As Variant
    
    Set olNS = olApp.GetNamespace("MAPI")
    Set olFolder = olNS.GetDefaultFolder(olFolderSentMail)
    Set olItems = olFolder.Items.Restrict("[SenderEmailAddress] = 'Dr_Demento@CompuServe.com'")
    
    For Each vItem In olItems
        If TypeName(vItem) = "MailItem" Then
            If InStr(vItem.Subject, "RE:") = 0 And InStr(vItem.Subject, "FW:") = 0 Then
                'etc
                '
                '
            End If
        End If
    Next vItem

To test when it was sent on...

Code:
vItem.SentOn

Hope this helps!
 
Upvote 0
Domenic,

I was having some issues until I structured the Restrict statement like an example done by The Scripting Guys. Don't know why that would make a difference using a separate assignment for olItems, but when I tried to use your example the loop was skipped entirely :confused::confused: Ah well, I'll take it!! I appreciate your time/efforts.

And for anyone who has a similar question in the future, to ensure that I'm only looking at emails sent today, the vItem.SentOn includes a time component and therefore I couldn't get a true condition until I used DateValue:
Code:
DateValue(vItem.SentOn) = DateValue(Date)

Thanks again, Domenic! I love this forum!!!!
 
Upvote 0
Don't know why that would make a difference using a separate assignment for olItems...

It shouldn't make a difference. I did test my code before posting it. But you've got it working so that's great.

Thanks again, Domenic!

You're very welcome! Glad I could help!

Cheers!
 
Upvote 0
One last question, Domenic.

How do I get a ReplyAll of the email once I've identified it? I have tried using With vItem.ReplyAll but all that does is create a new email with the same distro list and RE: in the subject line. I want to capture the text of the original email as well -- you know, like a real Reply!

Have a great weekend!
 
Upvote 0
Have a look at your options and make sure that the appropriate one is selected...

Code:
File > Options > Mail > Replies and forwards > When replying to a message > Include original message text
 
Upvote 0
That's the option I have selected :oops: I tried as Dmitry suggested here but instead of my new text above the original text, I get the original text only.

I will open a new post given the new difficulties and will post the code that I'm using (rather than keep you guessing!)

Thanks much, Domenic!
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,486
Members
449,455
Latest member
jesski

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