我正在尝试自动创建一堆Outlook规则。我正在使用Python 2.7,win32com和Outlook2007。为此,我必须创建一个新的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()

打印


没有任何
没有任何

我已经研究了在非动态模式下使用win32com时makepy生成的代码。类_MoveOrCopyRuleAction'Folder'字典中有一个_prop_map_put_条目,但除此之外,我很困惑。

最佳答案

使用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

我知道不是用win32com.client,也不是用IronPython!

关于python - 使用win32com设置属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7089496/

10-10 05:07