An Outlook user wanted to forward messages that were not replied to within a specific period of time. If the messages were still unreplied to later, he wanted to forward them to a different address.
To do this you need to check the last verb property (PR_LAST_VERB_EXECUTED) using propertyaccessor. If the last verb is not 102 (reply) or reply all (103) then check the received date. If it's older than 7 days then forward to address1; if older than 3 days, forward to address2. (By checking for older messages first, we avoid forwarding the same message to address2 a second time.)
More propertyaccessors for email can be found at Read MAPI properties not exposed in Outlook's Object Model
This macro runs only when you run it, but you could use the method described at Send an email when an Appointment reminder fires to trigger it using a reminder.
To change the folder the macro checks, see Working with VBA and non-default Outlook Folders. As written it checks the default Inbox but it can check in any folder, including a shared mailbox's Inbox.
Sub ForwardAgedMail() Dim objOutlook As Outlook.Application Dim objNamespace As Outlook.NameSpace Dim objSourceFolder As Outlook.MAPIFolder Dim objVariant As Variant Dim intCount As Integer Dim intDateDiff As Integer Dim propertyAccessor As Outlook.propertyAccessor Dim myForward As Outlook.MailItem 'On Error Resume Next Set objOutlook = Application Set objNamespace = objOutlook.GetNamespace("MAPI") Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox) ' use a subfolder under Inbox For intCount = objSourceFolder.Items.Count To 1 Step -1 Set objVariant = objSourceFolder.Items.Item(intCount) DoEvents If objVariant.Class = olMail Then Set propertyAccessor = objVariant.propertyAccessor If Not propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 102 _ And Not propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 103 Then intDateDiff = DateDiff("d", objVariant.SentOn, Now) ' I'm using 7 days, adjust as needed. If intDateDiff > 7 Then 'forward message Set myForward = objVariant.Forward myForward.To = "alias1@domain.com" myForward.Display ElseIf intDateDiff > 3 Then Set myForward = objVariant.Forward myForward.To = "alias2@domain.com" myForward.Display ' .send End If End If End If Next End Sub
Don't count weekends
Removing weekends (or holidays) from the count is more difficult. If the date span is 5 business days, this snippet should work. It checks the sent date and adds 2 to account for the weekend days.
Dim sentDate As Date sentDate = objVariant.SentOn Select Case Weekday(sentDate, vbUseSystemDayOfWeek) Case vbSunday sentDate = DateAdd("d", 1, sentDate) Case vbSaturday sentDate = DateAdd("d", 2, sentDate) Case vbFriday sentDate = DateAdd("d", 2, sentDate) Case vbThursday sentDate = DateAdd("d", 2, sentDate) Case vbWednesday sentDate = DateAdd("d", 2, sentDate) Case vbTuesday sentDate = DateAdd("d", 2, sentDate) Case vbMonday sentDate = DateAdd("d", 2, sentDate) End Select intDateDiff = DateDiff("d", sentDate, Now)
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
More Information
To view the last verb and last verb executed time in your email folders, see How to display the sender's email address in Outlook.
This macro is based off of the macro at Use a Macro to Move Aged Email in Outlook
DateDiff Function (Visual Basic) (MSDN)
Hi Diane,
I'm using your example to collect all emails which I have sent to other recipients without them replying, into a separate folder. I manage to do that, however the emails in my new folder are only a handfull of the ones sent. I only see some mails which I've sent to myself or to my other email and other that were auto-generated from SharePoint on my behalf.
Is it even possible to use the NOTEIVERB_REPLYTOSENDER or NOTEIVERB_REPLYTOALL property to accomplish this?
Hi, i have your code for copy older emails and i added 2 if statements to check for sender address and subject but it doesn't work. Any help is welcomed. Thank you.
For intCount = objSourceFolder.Items.Count To 1 Step -1
Set objVariant = objSourceFolder.Items.Item(intCount)
DoEvents
If objVariant.Class = olMail Then
If objVariant.SenderAddress = "Alin@yahoo.com" Then
If objVariant.Subject = "Client update" Then
Set propertyAccessor = objVariant.propertyAccessor
If Not propertyAccessor.GetProperty("https://schemas.microsoft.com/mapi/proptag/0x10810003") = 102 _
And Not propertyAccessor.GetProperty("https://schemas.microsoft.com/mapi/proptag/0x10810003") = 103 Then
intDateDiff = DateDiff("d", objVariant.SentOn, Now)
Set objDestFolder = objNamespace.Folders("Mailbox - Marius").Folders("Inbox").Folders("zile arhivare").Folders("Ieri")
If intDateDiff > -1 Then
Set Copy = objVariant.Copy
Copy.MOVE objDestFolder
End If
End If
End If
End If
End If
This works almost exactly the way I need, however, is there a way to forward messages from a specific sender that were not replied to. For example, anytime I receive a message from A@A.com and i do not reply within 3 days it forwards the message, but if I receive a message from B@A.com it does nothing?
Thank you so much for your help.
You can use an if statement after checking for the olmail type -
If objVariant.Class = olMail Then
If objvariant.senderaddress <> "a@a.com" then
exit sub
end if
hi, it worked until now and now it gives me an error at : For intCount = objSourceFolder.Items.Count To 1 Step -1 .eny help is wolcomed. thank you.
Hi, i made it :D ( thank you verry much ) . But if you will allow, i want to ask you if there is an way to search in a shared folder an email with the subject from an excel. the ideea is that i have some 200 names in an excel and i must manually search in an shared folder for email with the client name in the subject. Is there a way to make the search automatic and warn me if i have emails for a client name. Thank you.
Hi how can i make this code to search in a shared inbox for those email that were not replied to? Now it searches in the personal inbox and i want to search in an office shared inbox. any help is welcomed. Thank you.
This tells it to look in the default inbox:
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
replace it with this:
Set objSourceFolder = GetFolderPath("Shared mailbox name\Inbox")
and get the GetFolderpath function at https://www.slipstick.com/developer/code-samples/use-macro-assign-messages-shared-mailbox/#getfolderpath
Dear Diana,
Thanks for the response. How about being use of combination of ConversationIndex and ConversationID?
It could work, but not all mail clients/servers leave the conversation id in place. Adding a guid to the message body and searching on it would also work - when messages arrive, a macro looks for the guid and clears the matching flag or deletes the task. You could remove the guid from the body when a match is found so the macro won't keep searching for a match as this can slow outlook down if you are sending 200 messages a day.
Dear Diana,
I have another question – sent on average about 100 to 200 messages / emails on daily basis. What I need is that an email / message sent from my account, if un-replied by recipient after two days after being sent, will be re-sent again with addition of a sentence such as reminder. How is that possible to be achieved?
Checking for replies to messages you sent is more difficult and I don't have any code samples that can do it. It think Sperry software has a utility that monitors for replies and notifies you - i don't know if it can send a reminder to the recipient.