Setting a property using win32com

I am trying to create a bunch of Outlook rules automatically. I use Python 2.7, win32com and Outlook 2007. To do this, I have to create a new Rule object and specify a folder for its move action. However, I cannot set the Folder property successfully - it just remains None, despite the fact that I am giving the object of the desired 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() 

Print

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

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

+4
source share
2 answers

Try SetFolder ()

I think when reading the code fluently try SetFolder (move, foo_folder)

win32com does some awesome magic, but sometimes COM objects just win. when the object cannot follow the pythonic convention, a setter and getter are created behind the scenes from the 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 usage groups ...

+2
source

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 this not with win32com.client, but not with IronPython!

0
source

All Articles