使用 win32com 设置属性

发布于 2024-11-30 00:24:09 字数 1098 浏览 1 评论 0原文

我正在尝试自动创建一堆 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 Object Library.MAPIFolder instance at 0x51634584>
None
None

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

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 folder for its move action. However, I can't set the Folder property successfully -- it just stays None despite me giving an object of the right type.

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()

Prints

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

I've looked at the code generated by makepy when using win32com in non-dynamic mode. The class _MoveOrCopyRuleAction has an entry for 'Folder' in its _prop_map_put_ dict, but other than that I'm stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-07 00:24:09

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

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!

夜血缘 2024-12-07 00:24:09

尝试 SetFolder()

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

win32com 做了一些惊人的魔法,但有时 COM 对象只是打败了它。
当对象无法遵循 pythonic 约定时,会在幕后创建 Set{name} Get{name} 形式的 setter 和 getter,

请参阅: http://permalink.gmane.org/gmane.comp.python.windows/3231
注意 - Mark Hammonds 如何调试 com 是无价的 - 这个东西只是隐藏在使用组中......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文