使用win32com设置属性

PAG*_*PAG 5 python outlook win32com

我正在尝试自动创建一堆Outlook规则.我正在使用Python 2.7,win32com和Outlook 2007.为此,我必须创建一个新的Rule对象并为其移动操作指定一个文件夹.但是,我无法成功设置Folder属性 - 尽管我给出了一个正确类型的对象,它仍然保持None.

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()
Run Code Online (Sandbox Code Playgroud)

打印

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

我查看了makepy在非动态模式下使用win32com时生成的代码.该课程在其词典中_MoveOrCopyRuleAction有一个条目,但除此之外,我很难过.'Folder'_prop_map_put_

Ben*_*n.T 2

comtypes.client而不是win32com.client你可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

我知道这不是 win32com.client 的问题,但也不是 IronPython 的问题!