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 Object Library.MAPIFolder instance at 0x51634584>
なし
なし

makepy非動的モードでwin32comを使用するときに生成されるコードを見てきました。クラスにはそのdictに_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}の形式で作成されます。

参照: http: //permalink.gmane.org/gmane.comp.python.windows/3231NB- マークハモンズcomのデバッグ方法は貴重です-このようなものはusegroupsに隠されています...

于 2011-08-17T16:19:02.443 に答える