I have a Python script to create outlook rules based on some details in an Excel spreadsheet (rule name, from address, folder to move to and category).
I've been able to get the MoveToFolder action to work, but can't figure out the AssignToCategory one.
This is what the data in the excel file looks like. I bring it in as a list of strings (1 string per row) and use split to get the individual values.
Here is the Python script:
import comtypes.client as cc
import Read_Excel as xl
# Get details for rules to create from Excel File
rules_to_proces = xl.load_rules(r"C:\Users\myname\Documents\Outlook_Rules.xlsx")
# Create Outlook object
olApp = cc.CreateObject("Outlook.Application")
# Get Outlook collection of rules
olRules = olApp.Session.DefaultStore.GetRules()
# Get Account address
olAccount = olApp.GetNamespace('MAPI').Accounts.Item(1).DisplayName
# For each rule pulled in from the Excel file
for pRule in rules_to_proces:
# Set the rule name, from email address, folder/s and category based on data pulled in from Excel
olRuleName = pRule.split(';')[0]
olRuleAddress = pRule.split(';')[1]
olRuleFolder = pRule.split(';')[2]
olRuleCategory = pRule.split(';')[3]
# Create a new rule in the Outlook rules collection
olRule = olRules.Create(olRuleName, 0)
# Set the from condition to the email address and name of the sender and enable the condition
olFromCondition = olRule.Conditions.From
olFromCondition.Enabled = True
olFromCondition.Recipients.Add(olRuleAddress)
olFromCondition.Recipients.Add(olRuleName)
olFromCondition.Recipients.ResolveAll
# Set the root folder for the account
olRootFolder = olApp.GetNamespace('MAPI').Folders.Item(olAccount)
# Get a count of how many folders deep the string from Excel file has
# i.e. Inbox/Folder = 2, Inbox/Folder/Folder = 3 etc.
olFolderCount = len(olRuleFolder.split('/'))
# Set destination folder up to 5 folders deep, depending on olFolderCount
if olFolderCount == 1:
olDestinationFolder = olRootFolder\
.Folders[olRuleFolder.split(r'/')[0]]
elif olFolderCount == 2:
olDestinationFolder = olRootFolder\
.Folders[olRuleFolder.split(r'/')[0]]\
.Folders[olRuleFolder.split(r'/')[1]]
elif olFolderCount == 3:
olDestinationFolder = olRootFolder\
.Folders[olRuleFolder.split(r'/')[0]]\
.Folders[olRuleFolder.split(r'/')[1]]\
.Folders[olRuleFolder.split(r'/')[2]]
elif olFolderCount == 4:
olDestinationFolder = olRootFolder\
.Folders[olRuleFolder.split(r'/')[0]]\
.Folders[olRuleFolder.split(r'/')[1]]\
.Folders[olRuleFolder.split(r'/')[2]]\
.Folders[olRuleFolder.split(r'/')[3]]
elif olFolderCount == 5:
olDestinationFolder = olRootFolder\
.Folders[olRuleFolder.split(r'/')[0]]\
.Folders[olRuleFolder.split(r'/')[1]]\
.Folders[olRuleFolder.split(r'/')[2]]\
.Folders[olRuleFolder.split(r'/')[3]]\
.Folders[olRuleFolder.split(r'/')[4]]
else:
print("Folder tree too deep.")
# Enable move to folder action, set to enabled and set destination folder
olMove = olRule.Actions.MoveToFolder
olMove.__MoveOrCopyRuleAction__com__set_Enabled(True)
olMove.__MoveOrCopyRuleAction__com__set_Folder(olDestinationFolder)
# below is what causes the error
# enable assign to category action, set to enabled and set category to be assigned
olCategory = olRule.Actions.AssignToCategory
olCategory.__AssignToCategoryRuleAction__com__set_Enabled(True)
olCategory.__AssignToCategoryRuleAction__com__set_Categories(olRuleCategory)
# save the rules collection with the new rule added
olRules.Save()
Error Output:
Traceback (most recent call last):
File "C:\Users\myname\PycharmProjects\pythonProject1\Outlook2.py", line 79, in <module>
olCategory.__AssignToCategoryRuleAction__com__set_Categories(olRuleCategory)
_ctypes.COMError: (-2147352571, 'Type mismatch.', (None, None, None, 0, None))
Process finished with exit code 1
The Action.AssignToCategory.Categories object type is tuple.
If I just add tuple, I get a slightly different error:
Traceback (most recent call last):
File "C:\Users\myname\PycharmProjects\pythonProject1\Outlook2.py", line 79, in <module>
olCategory.__AssignToCategoryRuleAction__com__set_Categories(tuple(olRuleCategory))
_ctypes.COMError: (-2147024809, 'The parameter is incorrect.', ('Sorry, something went wrong. You may want to try again.', 'Microsoft Outlook', None, 0, None))
Process finished with exit code 1
I'm still very new to python so this is really melting my brain. Any help that can be offered would be greatly appreciated?
I've tried as many different versions as I can think of/find online such as:
olCategory.__AssignToCategoryRuleAction__com__set_Categories(**[**olRuleCategory**]**)
olCategory.Categories = olRuleCategory
olCategory.Categories = **[**olRuleCategory**]**
olRuleCategory = tuple(olRuleCategory)
The way below results in the code executing without error, but there is nothing in the rule about the category.
olCategory.__AssignToCategoryRuleAction__com__set_Enabled = True
olCategory.__AssignToCategoryRuleAction__com__set_Categories = olRuleCategory
