Solved

VB for Outlook to forward unread mail?

Posted on 2008-06-13
13
825 Views
Last Modified: 2010-12-19
I am wanting Outlook to forward mail to another address if the message has not been read in X number of minutes.  I would need this to be a scheduled task to run every X number of minutes, and then forward the message and mark the message as read so it doesn't forward it again.

Is this possible?
0
Comment
Question by:slthom
  • 7
  • 6
13 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi, slthom.

Yes, it's possible with a few caveats.

1.  It's not possible to mark a sent message as read.  It'll require flagging or using some other means of identification.
2.  Sending messages from code is going to invoke Outlook's built-in security (unless you're using Outlook 2007).  The result will be a popup dialog-box warning that an application is trying to send on your behalf.  You have to click Yes to allow it to continue.  That kills this as an automatic process.  Security cannot be turned off, but using third-party tools (not all free) you can work around it.

How would you want to handle messages sent to several recipients?  Would you forward if any of them haven't read it, or only if none of them had read it?

I can supply the code and instructions if you want to proceed.
0
 

Author Comment

by:slthom
Comment Utility
Thanks for the response.

>>>>How would you want to handle messages sent to several recipients?
I only need it to forward to an alternate address for me, and me only.

I just need it to forward if I am not sitting at my computer checking and reading mail.  If I read the messages from my Outlook, I don't want those forwarded.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
I understand, but that wasn't my question.  Say that you send a message addressed to five recipients.  The task fires and four of the five recipients have read the message.  Should the script forward the message to you?

That aside, the script below should do what you've described.  I say should because I haven't tested it.  Follow these instructions to use this.

1.  Open Notepad
2.  Copy the code below and paste it into Notepad
3.  Edit the code as needed.  At a minimum you'll need to change the address that messages are forwarded to.
4.  Save the file with a .vbs extension
5.  Create a task using Windows Task Scheduler
6.  Set the task to run this script
7.  Set the task to run at whatever interval you choose
8.  Be sure to leave Outlook running
9.  When the task fires it runs the script.  The script processes all the messages in Sent Items.  If a message hasn't been tagged as processed, then the script checks to see if all the recipients have read the message.  If any of them haven't, then it forwards the message to you.
    Const olFolderSentMail = 5

    Const olMail = 43

    Const olGreenFlagIcon = 3

    Const olTrackingRead = 6

    Dim olkApp, olkFolder, olkMsg, olkForward, olkRecipient

    'Get the open Outlook application.  Outlook has to be open for this to work.

    Set olkApp = GetObject(, "Outlook.Application")

    'Grab the Sent Items folder

    Set olkFolder = olkApp.Session.GetDefaultFolder(olFolderSentMail)

    'Check each message in Sent Items

    For Each olkMsg In olkFolder.Items

        'If this is a message (we want to skip meeting requests, task requests, etc.)

        If olkMsg.Class = olMail Then

            'Is the message flagged in green?  If yes, then skip it.

            If olkMsg.FlagIcon <> olGreenFlagIcon Then

                'Check each recipient

                For Each olkRecipient In olkMsg.Recipients

                    'If the recipient has not read the message ...

                    If olkRecipient.TrackingStatus <> olTrackingRead Then

                        'Forward the message

                        Set olkForward = olkMsg.Forward

                        'Change the address on the following line

                        olkForward.Recipients.Add "JohnDoe@company.com"

                        olkForward.Recipients.ResolveAll

                        olkForward.Send

                        'Flag the original in green so it'll be ignored by the next pass

                        olkMsg.FlagIcon = olGreenFlagIcon

                        olkMsg.Save

                        Exit For

                    End If

                Next

            End If

        End If

    Next

    Set olkRecipient = Nothing

    Set olkForward = Nothing

    Set olkMsg = Nothing

    Set olkFolder = Nothing

    Set olkApp = Nothing

Open in new window

0
 

Author Comment

by:slthom
Comment Utility
My only question is why is this script searching through my "Sent Items?"  

I am wanting any mail that "I" haven't read to be forwarded to an alternate address (my cell phone).  

That way, when I am not sitting at my computer reading mail, I get messages on my cell phone that I haven't read.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Sorry, I thought you wanted messages forwarded to you that had not been read by the recipient in a certain amount of time.  For example, you send a message to John Doe and ask for a read receipt.  If John hasn't read the message in 10 minutes you want to be notified.  That's why in my first post I said things like, "It's not possible to mark a sent message as read" and "How would you want to handle messages sent to several recipients?"  When you didn't correct me I thought I had it right.  I'll revise the script to act on messages that you haven't read and repost.
0
 

Author Comment

by:slthom
Comment Utility
Sorry about that BlueDevil.  I misinterpreted what you originally said.  Sorry again my friend!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
No problem.  Here's the revised code.
    Const olFolderInbox = 6

    Const olMail = 43

    Dim olkApp As Outlook.Application, _

        olkFolder As Outlook.MAPIFolder, _

        olkMsg As Outlook.MailItem, _

        olkForward As Outlook.MailItem

    'Get the open Outlook application

    Set olkApp = GetObject(, "Outlook.Application")

    'Grab the Inbox folder

    Set olkFolder = olkApp.Session.GetDefaultFolder(olFolderInbox)

    'Check each message in the inbox

    For Each olkMsg In olkFolder.items

        'If this is a message (we want to skip meeting requests, task requests, etc.)

        If olkMsg.Class = olMail Then

            'Is the message is unread ...

            If olkMsg.UnRead Then

                '... forward the message

                Set olkForward = olkMsg.Forward

                olkForward.Recipients.Add "JohnDoe@company.com"

                olkForward.Recipients.ResolveAll

                olkForward.Send

                'Flag the original as read

                olkMsg.UnRead = False

                olkMsg.Save

            End If

        End If

    Next

    Set olkForward = Nothing

    Set olkMsg = Nothing

    Set olkFolder = Nothing

    Set olkApp = Nothing

Open in new window

0
 

Author Comment

by:slthom
Comment Utility
Produces an error:

Line:  3
Char:  12
Error:  Expected end of statement
Code:  800A0401
Source:  Microsoft VBScricpt compilation error
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Sorry, that's what I get for writing in VBA and transferring to VBScript.  Change these lines

    Dim olkApp As Outlook.Application, _
        olkFolder As Outlook.MAPIFolder, _
        olkMsg As Outlook.MailItem, _
        olkForward As Outlook.MailItem

to

    Dim olkApp, olkFolder, olkMsg, olkForward
0
 

Author Comment

by:slthom
Comment Utility
OK, couple of things:

1.  It works with Outlook, but I get the security box that pops up telling me that outlook is trying to send a message and I have to click yes to continue.  That defeats the purpose since I am needing it to forward with no user intervention

2.  I also tried it with Outllook Express being my default mail program and I get error:

Line:  5
Char:  1
Error:  ActiveX component can't create object:  'GetObject'
Code:  800A01AD

0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
1.  I know.  That's why in my initial post I said, "2.  Sending messages from code is going to invoke Outlook's built-in security (unless you're using Outlook 2007).  The result will be a popup dialog-box warning that an application is trying to send on your behalf.  You have to click Yes to allow it to continue.  That kills this as an automatic process.  Security cannot be turned off, but using third-party tools (not all free) you can work around it."

2.  No, this won't work with Outlook Express because it is not programmable.  Only the full Outlook client is programmable.
0
 

Author Comment

by:slthom
Comment Utility
Oh yeah, forgot about that.  Well, I found this and it seems to work (it bypasses Outlooks security):

http://www.office-addins.com/-outlook-addins/advanced-security-for-outlook.html

Thanks for the help BlueDevilFan
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
You're welcome.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you don't know how to downgrade, my instructions below should be helpful.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Viewers will learn the different options available in the Backstage view in Excel 2013.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now