Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2014, 01:42 AM
steve_m steve_m is offline Creating a large number of Outlook Rules Windows 7 64bit Creating a large number of Outlook Rules Office 2007
Novice
Creating a large number of Outlook Rules
 
Join Date: Feb 2012
Location: UK
Posts: 3
steve_m is on a distinguished road
Default Creating a large number of Outlook Rules

Hello,

After a catastrophic event I lost all the Outlook rules I had in place to move emails with specific subject lines into there appropriate folders.

Is there a way to create a large number of rules (132 to be precise) in a scripted way?

The rules are very simple
with WIN CI_Overnight_Build in the subject
move it to the Win Overnight folder
except if the subject contains 'RE:' or 'FW:'
Regards,
Steve
Reply With Quote
  #2  
Old 01-08-2015, 10:13 AM
steve_m steve_m is offline Creating a large number of Outlook Rules Windows 8 Creating a large number of Outlook Rules Office 2013
Novice
Creating a large number of Outlook Rules
 
Join Date: Feb 2012
Location: UK
Posts: 3
steve_m is on a distinguished road
Default Solution Found.

I have managed to find a way to do this, so I thought I’d share the solution in case anyone else wants to do something similar.

The method I have used is a PowerShell script. My starting point was a blog entry by Dan Jansson, once I was familiar with that I started to play around with it until I ended up with the solution to suit my problem.

Once thing to be aware of. The PowerShell command prompt and the Outlook session need to be at the same permission level, so if you have started the PowerShell prompt using 'Run as Administrator' you will need to do the same for Outlook [article here].

Also to use it you will need to import the module first.

Code:
Import-Module .\CreateOutlookRule.ps1
Here is the code for the CmdLet I came up with, I am very much a beginner with PowerShell so I'm sure it is far from perfect

Code:
<#
.Synopsis
This function will create Outlook rules to move emails into folders based on the subject line.

.Description
This function will create Outlook rules to move emails into folders based on the subject line.
It will include an exception for emails that contain RE: or FW: in the subject line.

The rule will be 

  with [-SubjectWords] in the subject
  move it to the [-RedirectFolder]
  except if the subject contains 'RE:' or 'FW:'


 ACKNOWLEDGEMENTS: Thanks to Dan Jansson, whose script I used as the basis for this script.  His script 
 can be found on his blog 'Using PowerShell to create rules in Outlook'

.Parameter RuleName
This parameter accepts a string to be used as the name of the rule
This parameter is Mandatory

.Parameter RedirectFolder
This parameter accepts a string, or an array of strings, to point to the folder where the emails should
be moved to.
This parameter is Mandatory

.Parameter SubjectWords
This parameter accepts a string, or an array of strings, to specify the subject line to check for.
This parameter is Mandatory

.Example
Create-OutlookRule "WIN CI_Overnight_Build" @("Personal Folders","CI","Win Overnight") "WIN CI_Overnight_Build"

Create a rule called WIN CI_Overnight_Build, that will move emails whose subject contains "WIN CI_Overnight_Build" 
into the '\\Personal Folders\CI\Win Overnight' folder.

.Example
Create-OutlookRule "StatusCheck" "Build Status" @("Build Status","Status of Builds")

Create a rule called 'StatusCheck', that will move emails whose subject contains "Build Status" or "Status of Builds" 
into the 'Status' folder directly under the main inbox

#>
Function Create-OutlookRule
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [string]$RuleName,

        # Outlook Folder to move emails into.  Can accept multiple items to build the 
        # path to a subfolder
        [Parameter(Mandatory=$True)]
        [string[]]$RedirectFolder,
        
        # Words or phrase to search the subject line
        [Parameter(Mandatory=$True)]
        [string[]]$SubjectWords
    )

    Add-Type -AssemblyName microsoft.office.interop.outlook 
    $olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
    $olRuleType = "Microsoft.Office.Interop.Outlook.OlRuleType" -as [type]
    $outlook = New-Object -ComObject outlook.application
    $namespace  = $Outlook.GetNameSpace("MAPI")

    # Set the $inbox variable to point to the main Outlook inbox
    $inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox)

    # Set $targetFolder to just $namespace as we will use $RedirectFolder to build this folder path
    $targetFolder = $namespace
    
    Foreach($Folder in $RedirectFolder){
            Try{
                # Check the size of $RedirectFolder
                #  - If it is greater than 1 then I'll assume the full path has been specified in $RedirectFolder
                #  - If it is NOT greater than 1 I'll assume the target folder is directly under the Inbox.
                if ($RedirectFolder.count -gt 1){                    
                    $targetFolder = $targetFolder.Folders.Item($Folder)
                } else {
                    $targetFolder = $inbox.Folders.Item($Folder)
                }
            }
            Catch{
               # Use Write-Host to give a meaningful error message, but make it look like an error
               # I tried using Write-Error but it wasn't displaying all the error message
               Write-Host "Error setting targetFolder.  It May not exist" -BackgroundColor Black -ForegroundColor Red

               # Throw the exception to display the error and terminate the execution
               throw
            }
    }

    # Intialise the rules object
    $rules = $outlook.session.DefaultStore.GetRules()
    $rule = $rules.Create($RuleName,$olRuleType::OlRuleReceive)

    # Intialise the Subject conditions
    $SubjectCondition = $rule.Conditions.Subject
    $SubjectCondition.Enabled = $true
    $SubjectCondition.Text = $SubjectWords

    # Add an exception to the rule for subject line that contain "RE:" or "FW:"
    $SubjectException = $rule.Exceptions.Subject
    $SubjectException.Enabled = $true
    $SubjectException.Text = @("RE:", "FW:")
     
    Try{
        $d = [System.__ComObject].InvokeMember(
            "EntryID",
            [System.Reflection.BindingFlags]::GetProperty,
            $null,
            $targetFolder,
            $null)
        $MoveTarget = $namespace.getFolderFromID($d)
    }
    Catch {
        # Use Write-Host to give a meaningful error message, but make it look like an error
        # I tried using Write-Error but it wasn't displaying all the error message
        Write-Host "An error occured when setting the MoveTarget" -BackgroundColor Black -ForegroundColor Red

        # Throw the exception to display the error and terminate the execution
        throw
    }
    
    Try{
        $MoveRuleAction = $rule.Actions.MoveToFolder
        [Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember(
            "Folder",
            [System.Reflection.BindingFlags]::SetProperty,
            $null,
            $MoveRuleAction,
            $MoveTarget)
        $MoveRuleAction.Enabled = $true
    }
    Catch {
        # Use Write-Host to give a meaningful error message, but make it look like an error
        # I tried using Write-Error but it wasn't displaying all the error message
        Write-Host "An error occured when setting the MoveRuleAction" -BackgroundColor Black -ForegroundColor Red

        # Throw the exception to display the error and terminate the execution
        throw
    }

    $rules.Save()
}
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating multiple conditional rules on one column secoo140 Excel 0 10-07-2013 12:13 PM
Creating rules donnavoytko Outlook 1 07-31-2012 05:54 AM
Creating a large number of Outlook Rules I want to send email to large number of people, with different addressing sanketmlad Outlook 1 10-06-2011 09:32 AM
Creating a large number of Outlook Rules Creating Rules to block Senders by "Nickname" rather than email address RavnResn Outlook 1 05-24-2011 01:35 AM
Schedule Assistant for Large number of Attendéendes outlooker Outlook 0 02-08-2011 02:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft