小编典典

使用win32com设置属性

python

我正在尝试自动创建一堆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.gen_py.Microsoft Outlook 12.0对象库.MAPIFolder实例位于0x51634584>
没有
没有

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


阅读 219

收藏
2021-01-16

共1个答案

小编典典

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

2021-01-16