'How to search recipients of previously sent mail?
I have a code which checks the subject in my e-mail and warns me if I already sent an email with this subject. I experienced it would be smarter if it checks for the email address.
I tried substituting "subject" with "recipients" but without success.
Public Sub Application_ItemSend(ByVal thisItem As Object, Cancel As Boolean)
Dim ns As Outlook.NameSpace
Dim olfolder As MAPIFolder
Dim Item As Object
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set firstFolder = olNs.Folders("[email protected]") ' name of my shared inbox
Set olfolder = firstFolder.Folders("sent items")
' iterate thru emails
For Each Item In olfolder.Items
' check subject content & date difference
If InStr(Item.Subject, thisItem.Recipients) And DateDiff("m", Item.SentOn, Now) < 1 Then
' added this part
If MsgBox("already sent", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Message Text Warning") = vbNo Then
' cancel the email
Cancel = True
End If
Exit For
End If
Solution 1:[1]
Just replace thisItem.Subject
to thisItem.To
edit: Nathan_Sav said it all :) .Recipents
property returns an array so you can loop through it, might as well use .To
which will return all recipients separated by semicolon
edit2: note there are two other properties .cc
and .bcc
if you use them, whilst .Recipients
array include all of them
Solution 2:[2]
Firstly, do not ever loop through all items in a folder - this is horribly inefficient. Use Items.Find
with a restriction on the Subject
and SentOn
properties.
For the recipients, you can use a restriction on the To
property, but keep in mind that on the MAPI level it translates to a restriction on the PR_DISPLAY_TO
property, which may or may not include the actual email address. OOM does not create subrestrictions on the PR_MESSAGE_RECIPIENTS
property. If using Redemption (I am its author) is an option, its version of RDOItems.Find/Restrict
allows to specify Recipients
, To
, CC
, BCC
properties in a query and creates an appropriate restriction on the email address and name of the message recipients.
In the worst case you can restrict on the Subject
and SentOn
properties using Items.Find/FindNext
or Items.Restrict
, and then explicitly loop through the Recipients collection of the returned matches.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |