Link to home
Start Free TrialLog in
Avatar of slthom
slthom

asked on

VB for Outlook to forward unread mail?

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?
Avatar of David Lee
David Lee
Flag of United States of America image

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.
Avatar of slthom
slthom

ASKER

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.
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

Avatar of slthom

ASKER

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.
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.
Avatar of slthom

ASKER

Sorry about that BlueDevil.  I misinterpreted what you originally said.  Sorry again my friend!
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slthom

ASKER

Produces an error:

Line:  3
Char:  12
Error:  Expected end of statement
Code:  800A0401
Source:  Microsoft VBScricpt compilation error
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
Avatar of slthom

ASKER

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

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.
Avatar of slthom

ASKER

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
You're welcome.