Avatar of jamorlando
jamorlando
Flag for United States of America asked on

Create "Rules and Alerts" in Outlook 2007 using VBScript

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:
https://www.experts-exchange.com/questions/26605565/Create-Outlook-Rules-And-Alerts-with-Powershell.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
VB ScriptOutlook

Avatar of undefined
Last Comment
David Lee

8/22/2022 - Mon
David Lee

Hi, Jamie.

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

ASKER
The rule would take a message sent to user X and move the message to folder Y.
ASKER CERTIFIED SOLUTION
David Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jamorlando

ASKER
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,
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
David Lee

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

ASKER
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?
David Lee

Is the folder visible in Outlook's navigation pane (i.e. the folder list)?  Is this a folder in your mailbox?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jamorlando

ASKER
Yes it's visible in the left pane.
It is a folder in the inbox.
David Lee

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

ASKER
Sure thing Spam inbox
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
David Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jamorlando

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

Worked!

Thanks so much!
David Lee

You're welcome.  Glad I could help.
jamorlando

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Lee

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

ASKER
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?
David Lee

I doubt that'll make any difference, but there's no harm in trying.  
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jamorlando

ASKER
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/
jamorlando

ASKER
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!
David Lee

Excellent.  Glad you got it working.  Thanks for sharing the fix.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.