PDA

View Full Version : Perform macro ONLY to certain TO addresses



lordterrin
02-18-2014, 12:09 PM
Hello,

I have the following macro:



Option Explicit


Private WithEvents olSentItems As Items

Private Sub Application_MAPILogonComplete()


Dim objNS As NameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing


End Sub


Private Sub Application_Startup()



End Sub


Private Sub olSentItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Dim prompt As String


Dim objRecip As Recipient


Set objRecip = Item.Recipients.Add


prompt$ = "Do you want to flag this message for followup?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Add flag?") = vbYes Then

With Item
.MarkAsTask olMarkThisWeek
' sets a due date for tomorrow
.TaskDueDate = Now + 1
.ReminderSet = True
.ReminderTime = Now + 1
.Save
End With
End If


End Sub

lordterrin
02-18-2014, 12:12 PM
It works perfectly - each time I send an email, it asks me if I want to set a reminder for myself to follow up. The thing is, I'd like to wrap this around an IF statement, where the macro ONLY executes if it's NOT certain names. About 50% of the email I send is to 2 addresses, and I never want to set reminders to follow up on those. I don't know how to program this though, as I'm not strong on Outlook-specific VBA


My thoughts were to put the IF statement here:

IF OBJECIP <> "emailhere" THEN


prompt$ = "Do you want to flag this message for followup?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Add flag?") = vbYes Then

With Item
.MarkAsTask olMarkThisWeek
' sets a due date for tomorrow
.TaskDueDate = Now + 1
.ReminderSet = True
.ReminderTime = Now + 1
.Save
End With
End If

END IF

but this doesn't work - as I obviously don't know the exact syntax for this. Any ideas?

westconn1
02-20-2014, 04:08 AM
Set objRecip = Item.Recipients.Add
here you add a recipient to the collection, but you never set any of his properties, so it is not used

assuming you are only sending your emails in most cases to a single recipient, you can try like


if item.recipients(1).name = "xxx" or item.recipients(1).name = "yyy" then
' do nothing
else
' your code
end ifif it was more than 2, i would probably use a select case instead of if else

if you want to use email address as criteria, the crlteria may be item.recipients(1).addressentry.address