Solved

VB for Outlook to forward unread mail?

Posted on 2008-06-13
13
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 21782316
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
ID: 21782776
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
ID: 21798834
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:slthom
ID: 21802850
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
ID: 21804481
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
ID: 21804512
Sorry about that BlueDevil.  I misinterpreted what you originally said.  Sorry again my friend!
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 21808154
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
ID: 21809186
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
ID: 21809285
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
ID: 21814226
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
ID: 21817179
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
ID: 21817299
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
ID: 21817360
You're welcome.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Mailbox Overload?
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

724 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