Setting a property using win32com

前端 未结 2 1810
攒了一身酷
攒了一身酷 2020-12-18 07:42

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 fol

相关标签:
2条回答
  • 2020-12-18 07:43

    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 ...

    0 讨论(0)
  • 2020-12-18 07:44

    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!

    0 讨论(0)
提交回复
热议问题