5

我正在尝试自动创建一堆 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()

印刷

<win32com.gen_py.Microsoft Outlook 12.0 对象库.MAPIFolder 实例位于 0x51634584>
没有任何
没有任何

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

4

2 回答 2

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

我知道它与 win32com.client 无关,但也与 IronPython 无关!

于 2018-03-22T19:30:58.023 回答
1

尝试 SetFolder()

我认为通过粗略阅读您的代码尝试 SetFolder(move, foo_folder)

win32com 有一些神奇的魔力,但有时 COM 对象会打败它。当对象不能遵循 pythonic 约定时,在幕后创建一个 set{name} Get{name} 形式的 setter 和 getter

请参阅:http://permalink.gmane.org/gmane.comp.python.windows/3231 NB - Mark Hammonds 如何调试 com 是无价的 - 这些东西只是隐藏在 usegroups 中......

于 2011-08-17T16:19:02.443 回答