使用 win32com 设置属性 [英] Setting a property using win32com

查看:40
本文介绍了使用 win32com 设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入 win32com.client从 win32com.client 导入常量为 consto = win32com.client.gencache.EnsureDispatch("Outlook.Application")规则 = o.Session.DefaultStore.GetRules()rule = rules.Create("Python 规则测试", const.olRuleReceive)条件 = rule.Conditions.MessageHeadercondition.Text = ('Foo', 'Bar')条件.启用 = 真root_folder = o.GetNamespace('MAPI').Folders.Item(1)foo_folder = root_folder.Folders['Notifications'].Folders['Foo']移动 = rule.Actions.MoveToFolder打印 foo_folder打印移动文件夹move.Folder = foo_folder打印移动文件夹# move.Enabled = True# rules.Save()

印刷品

<前><win32com.gen_py.Microsoft Outlook 12.0 Object Library.MAPIFolder 实例在 0x51634584>没有任何没有任何

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

解决方案

使用 comtypes.client 而不是 win32com.client 你可以做到:

import comtypes.cliento = comtypes.client.CreateObject("Outlook.Application")规则 = o.Session.DefaultStore.GetRules()rule = rules.Create("Python rule test", 0 ) # 0 是参数 olRuleReceive 的值condition = rule.Conditions.Subject # 我猜 MessageHeader 也能用condition.Text = ('Foo', 'Bar')条件.启用 = 真root_folder = o.GetNamespace('MAPI').Folders.Item(1)foo_folder = root_folder.Folders['Notifications'].Folders['Foo']移动 = rule.Actions.MoveToFoldermove.__MoveOrCopyRuleAction__com__set_Enabled(True) # 否则需要这行# 该文件夹未在 Outlook 中设置move.__MoveOrCopyRuleAction__com__set_Folder(foo_folder) # 设置目标文件夹rules.Save() # 将其保存在 Outlook 中

我知道它不适用于 win32com.client,但也不适用于 IronPython!

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.

解决方案

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!

这篇关于使用 win32com 设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆