Forum Discussion
cdfjdk
Jul 17, 2023Copper Contributor
Macro to bulk create Outlook rules from CSV file
I am new to Outlook VBA and have struggled for a day now to write a macro that creates rules from a CSV file in a shared mailbox with the trigger for Body or Subject in column 1 and the path to the f...
NikolinoDE
Jul 17, 2023Platinum Contributor
Here is the updated code with the corrections:
Sub ImportRulesFromCSVTest()
Dim sharedMailboxName As String
sharedMailboxName = "email address removed for privacy reasons"
Dim olApp As Outlook.Application
Set olApp = Outlook.Application
Dim olNamespace As Outlook.NameSpace
Set olNamespace = olApp.GetNamespace("MAPI")
Dim olRecipient As Outlook.Recipient
Set olRecipient = olNamespace.CreateRecipient(sharedMailboxName)
olRecipient.Resolve
Dim olStore As Outlook.Store
Set olStore = olNamespace.GetSharedDefaultFolder(olRecipient, olFolderInbox).Store
Dim olRules As Outlook.Rules
Set olRules = olStore.GetRules
Dim csvFilePath As String
csvFilePath = "C:\Users\xyz\Test4.csv"
Open csvFilePath For Input As #1
Dim csvLine As String
Dim trigger As String
Dim folderPath As String
Do While Not EOF(1)
Line Input #1, csvLine
Dim csvValues() As String
csvValues = Split(csvLine, ",")
If UBound(csvValues) = 1 Then
trigger = csvValues(0)
folderPath = csvValues(1)
'Create rule
Dim olRule As Outlook.Rule
Set olRule = olRules.Create(trigger, olRuleClientOnly)
'Set trigger condition
Dim olRuleConditions As Outlook.RuleConditions
Set olRuleConditions = olRule.Conditions
Dim olSubjectCondition As Outlook.TextRuleCondition
Set olSubjectCondition = olRuleConditions.Subject
olSubjectCondition.Text = Array(trigger)
'Set exception exclude messages To (i.e. restrict rule to Cc and Bcc)
Dim oToCondition As Outlook.ToOrFromRuleCondition
Set oToCondition = olRuleConditions.ToOrFrom
With oToCondition
.Enabled = True
.Recipients.Add "email address removed for privacy reasons"
.Recipients.ResolveAll
End With
'Set move action
Dim olRuleActions As Outlook.RuleActions
Set olRuleActions = olRule.Actions
Dim olFolder As Outlook.Folder
Set olFolder = olStore.GetRootFolder.Folders(folderPath)
Dim olMoveOrCopyRuleAction As Outlook.MoveOrCopyRuleAction
Set olMoveOrCopyRuleAction = olRuleActions.MoveToFolder
With olMoveOrCopyRuleAction
.Enabled = True
.Folder = olFolder
End With
End If
Loop
Close #1
' Save the rules
olRules.Save
MsgBox "Rules imported", vbInformation
End SubPlease ensure that you have made the necessary adjustments, such as providing the correct CSV file path and the shared mailbox email address.
Remember to review and test the code with caution, and have a backup of your rules before running it to avoid any unintended changes. The text, steps and the code are the result of various AI's put together.
My answers are voluntary and without guarantee!
Hope this will help you.
cdfjdk
Jul 17, 2023Copper Contributor
NikolinoDE - thank you for trying to help, but might I suggest you kindly never post AI code without testing it first? Everyone can find AI code generators and AI code is flakey - the code you posted simply introduced errors into my code that were not there before.
- NikolinoDEJul 17, 2023Platinum ContributorThanks for the tip. That's why I pointed out at the end that the text, steps, and code are the result of various AIs put together. Pointed out that my answers are voluntary and above all without guarantee, since it was created by the AI's. Thanks again for the tip, I'll make it clearer next time. I wish you continued success and hope that you will quickly come up with a solution that will help you further than the one I sent.