Solved

Create "Rules and Alerts" in Outlook 2007 using VBScript

Posted on 2010-11-12
20
2,212 Views
Last Modified: 2012-05-10
I have Outlook 2007 and I'm trying to write a simple script that will create a new message rule automatically.  I have tried doing this for hours with Powershell and I am striking out due to an apparent bug.
See:
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26605565.html

Anyway, it's not really important what language this is written in.  Wanted to see if any of you have written this in VBScript or help point me in the right direction.

Thanks,
Jamie
0
Comment
Question by:jamorlando
  • 10
  • 10
20 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 34124173
Hi, Jamie.

Does the rule need to do something specific, or do you just want to see how to create rule this way?
0
 

Author Comment

by:jamorlando
ID: 34124201
The rule would take a message sent to user X and move the message to folder Y.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 34124388
Ok.  Here's the code for doing that.  This is VBscript.  Copy and paste the code into a file, edit it per the comments I included, and save it with a .vbs extension.
Const olRuleReceive = 0

Dim olkApp, olkSes, olkRules, olkRule, olkRuleActions, olkMoveRuleAction, olkFromCondition
Set olkApp = GetObject(,"Outlook.Application")
Set olkSes = olkApp.Session
Set olkRules = olkSes.DefaultStore.GetRules()
'On the next line change the rule name'
Set olkRule = olkRules.Create("My New Rule", olRuleReceive)
Set olkFromCondition = olkRule.Conditions.From
With olkFromCondition
    .Enabled = True
    'On the next line change the address the rule triggers on'
    .Recipients.Add "somone@company.com"
    .Recipients.ResolveAll
End With
Set olkMoveRuleAction = olkRule.Actions.MoveToFolder
With olkMoveRuleAction
    .Enabled = True
    'On the next line change the path to the target folder'
    Set .Folder = OpenOutlookFolder("Filing Cabinet\Miscellaneous")
End With
olkRules.Save True
'Uncomment the next line if you want to run the rule immediately on creation'
'olkRule.Execute True
Set olkFromCondition = Nothing
Set olkMoveRuleAction = Nothing
Set olkRuleActions = Nothing
Set olkRule = Nothing
Set olkRules = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit

Function OpenOutlookFolder(strFolderPath)
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders, varFolder, bolBeyondRoot
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = olkSes.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jamorlando
ID: 34124483
Thank you so much.
I am trying to get this to work on a test folder, a sub-folder called aaa in my Inbox.

In line 20, I changed this to:
    Set .Folder = OpenOutlookFolder("Inbox\aaa")

and I get the following error:
C:\Scripts\CreateRule.vbs(20, 5) Microsoft VBScript runtime error: Type mismatch: 'OpenOutlookFolder'

Any ideas what this is?  I have a terrible feeling this is the same type mismatch bug that I get when I'm trying to do this from Powershell.

Thanks,
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34124526
The folder path is probably not valid.  A folder path in Outlook is essentially the same as a folder path in the file system.  The one difference being that Outlook folder paths do not include a drive letter.  The path to a folder is a list of all the folders from the root to the target folder with each folder name separated from the preceding folder name by a backslash (i.e. \).  Consider the following folder structure:

Mailbox - Doe, John
    - Calendar
    - Inbox
    - Tasks
Personal Folders
    + Marketing
        + Proposals
        + Reviews
    + Projects
        + Project 1
        + Project 2

The path to "Inbox" is "Mailbox - Doe, John\Inbox".
The path to "Reviews" is "Personal Folders\Marketing\Reviews".
The path to "Project 1" is "Personal Folders\Projects\Project 1".
0
 

Author Comment

by:jamorlando
ID: 34124666
I tried:
Set .Folder = OpenOutlookFolder("Mailbox - SPAM\Inbox\aaa")
and
Set .Folder = OpenOutlookFolder("\\Mailbox - SPAM\Inbox\aaa")

Getting the same type mismatch error.  I've already created the folder, and the permissions are fine.

Any ideas?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34124690
Is the folder visible in Outlook's navigation pane (i.e. the folder list)?  Is this a folder in your mailbox?
0
 

Author Comment

by:jamorlando
ID: 34124735
Yes it's visible in the left pane.
It is a folder in the inbox.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34124764
Can you post a screenshot showing the folder structure?  It has to be a path issue.  I tested the code before posting and it worked perfectly.
0
 

Author Comment

by:jamorlando
ID: 34124838
Sure thing Spam inbox
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 500 total points
ID: 34125129
You have it right.  Line #20 should be

    Set .Folder = OpenOutlookFolder("Mailbox - SPAM\Inbox\aaa")

Since the folder is under the inbox you can also try

    Set .Folder = olkSes.GetDefaultFolder(6).Folders("aaa")

Are you logged into this account when the code runs?
0
 

Author Comment

by:jamorlando
ID: 34135949
Set .Folder = olkSes.GetDefaultFolder(6).Folders("aaa")

Worked!

Thanks so much!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34135963
You're welcome.  Glad I could help.
0
 

Author Comment

by:jamorlando
ID: 34138376
One more question if you don't mind:
This script worked perfectly when I ran it locally.  However when I go to run it remotely via the PSExec.exe utility, I get the following error on the line:
Set olkApp = GetObject(,"Outlook.Application")

It says:
C:\Scripts\CreateRule.vbs(4, 1) Microsoft VBScript runtime error: ActiveX component can't create object: 'GetObject'

I notice that the GetObject function takes 2 parameters and the first one is blank.  Is there something that should go in there (remote server name?) to make this work remotely?

Thanks,
Jamie
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34139160
When called with the syntax I've used GetObject retrieves the running instance of Outlook.  The first parameter can be a path to a file.  It is not a machine name.  Add the command

    On Error Resume Next

between lines 3 and 4.
0
 

Author Comment

by:jamorlando
ID: 34139386
Sorry to keep bothering you..
On Error Resume Next
makes the script run with no errors, however the rule never gets created.

When you say, the first parameter can be a path to a file, should I set up the GetObject line like:
Set olkApp = GetObject("C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE","Outlook.Application")

Any ideas?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34139474
I doubt that'll make any difference, but there's no harm in trying.  
0
 

Author Comment

by:jamorlando
ID: 34139510
It didn't.

However, I found this link which seems to describe my problem.  I'll check it out and let you know if that fixes it.
http://www.lewisroberts.com/2006/02/28/remote-scripting-activex-component-cant-create-object-on-wshcontroller-object/
0
 

Author Comment

by:jamorlando
ID: 34139577
I got it working.  I changed the line to:
Set olkApp = GetObject("","Outlook.Application")

I also ran some of the steps in the tutorial prior to this.  Not sure if that had anything to do with it working.

Here is the command line I used with PSExec to achieve this:
PsExec.exe \\SERVER -u "USERNAME" -p "PASSWORD" cscript C:\Scripts\CreateRule.vbs

Thanks for all your help!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 34139720
Excellent.  Glad you got it working.  Thanks for sharing the fix.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Blocking Read Receipt and Delivery Receipt request 3 21
Outlook 2016 18 51
outlook 365  Desktop Alert 6 38
One Contact List for Outlook, SharePoint, and Cell Phone 5 24
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

810 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