6

I'm trying to create a bunch of Outlook rules automatically. I'm using Python 2.7, win32com, and Outlook 2007. To do this I must create a new Rule object and specify a folder for its move action. However, I can't set the Folder property successfully -- it just stays None despite me giving an object of the right type.

import win32com.client
from win32com.client import constants as const

o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
rules = o.Session.DefaultStore.GetRules() 

rule = rules.Create("Python rule test", const.olRuleReceive) 
condition = rule.Conditions.MessageHeader 
condition.Text = ('Foo', 'Bar')
condition.Enabled = True

root_folder = o.GetNamespace('MAPI').Folders.Item(1)
foo_folder = root_folder.Folders['Notifications'].Folders['Foo']

move = rule.Actions.MoveToFolder
print foo_folder
print move.Folder
move.Folder = foo_folder
print move.Folder

# move.Enabled = True
# rules.Save()

Prints

<win32com.gen_py.Microsoft Outlook 12.0 Object Library.MAPIFolder instance at 0x51634584>
None
None

I've looked at the code generated by makepy when using win32com in non-dynamic mode. The class _MoveOrCopyRuleAction has an entry for 'Folder' in its _prop_map_put_ dict, but other than that I'm stumped.

2
  • I've got this working with IronPython. I needed to use: Outlook._MoveOrCopyRuleAction.Folder.SetValue(rule.Actions.MoveToFolder, folder). Nonetheless I'd still like to see a win32com answer. Commented Aug 19, 2011 at 16:35
  • I get an error object has no attribute 'DefaultStore' when running the above. Am I missing a rules store? WHat is one ? Also does the SetFolder() appraoch in win32com work on the objects IronPython uses? Commented Aug 22, 2011 at 8:32

2 Answers 2

2

With comtypes.client instead of win32com.client you can do:

import comtypes.client

o = comtypes.client.CreateObject("Outlook.Application")
rules = o.Session.DefaultStore.GetRules() 

rule = rules.Create("Python rule test", 0 ) # 0 is the value for the parameter olRuleReceive
condition = rule.Conditions.Subject # I guess MessageHeader works too
condition.Text = ('Foo', 'Bar')
condition.Enabled = True

root_folder = o.GetNamespace('MAPI').Folders.Item(1)
foo_folder = root_folder.Folders['Notifications'].Folders['Foo']

move = rule.Actions.MoveToFolder
move.__MoveOrCopyRuleAction__com__set_Enabled(True) # Need this line otherwise 
                                                    # the folder is not set in outlook
move.__MoveOrCopyRuleAction__com__set_Folder(foo_folder) # set the destination folder

rules.Save() # to save it in Outlook

I know it's not with win32com.client, but not with IronPython either!

Sign up to request clarification or add additional context in comments.

2 Comments

Any idea on how to do this from a second account? In other words, how to switch to a different account, for example session.accounts[2]
@usr unfortunately, I don't know like this and I don't have the right set up on my computer aymore to try it out.
2

Try SetFolder()

I think from a cursory reading of your code try SetFolder(move, foo_folder)

win32com does some amazing magic but at times COM objects just defeat it. when the object cannot follow the pythonic convention, behind the scenes a setter and getter is created of form Set{name} Get{name}

see: http://permalink.gmane.org/gmane.comp.python.windows/3231 NB - Mark Hammonds how to debug com is priceless - this stuff is just hidden in usegroups ...

1 Comment

SetFolder on which object? print move.SetFolder raises an AttributeError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.