[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 860
  • Last Modified:

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?
0
slthom
Asked:
slthom
  • 7
  • 6
1 Solution
 
David LeeCommented:
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
 
slthomAuthor Commented:
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
 
David LeeCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
slthomAuthor Commented:
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
 
David LeeCommented:
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
 
slthomAuthor Commented:
Sorry about that BlueDevil.  I misinterpreted what you originally said.  Sorry again my friend!
0
 
David LeeCommented:
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
 
slthomAuthor Commented:
Produces an error:

Line:  3
Char:  12
Error:  Expected end of statement
Code:  800A0401
Source:  Microsoft VBScricpt compilation error
0
 
David LeeCommented:
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
 
slthomAuthor Commented:
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
 
David LeeCommented:
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
 
slthomAuthor Commented:
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
 
David LeeCommented:
You're welcome.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now