【问题标题】:Python - comtypes (working) vs. pywin32 (not working) - Using python to create outlook rulesPython - comtypes(工作)与 pywin32(不工作) - 使用 python 创建 Outlook 规则
【发布时间】:2021-02-12 02:35:22
【问题描述】:

(Windows 10、Office 365、Python 3.7、Pycharm)

我能够使用 comtypes 在 Outlook 中成功创建规则:

import comtypes.client

o = comtypes.client.CreateObject("Outlook.Application")
rules = o.Session.DefaultStore.GetRules()
oRule = rules.Create("Test_Rule", 0)

condition = oRule.Conditions

oFromCondition = oRule.Conditions.From
oFromCondition.Enabled = True
oFromCondition.Recipients.Add("john@email.com")
oFromCondition.Recipients.ResolveAll

condition.Enabled = True
root_folder = o.GetNamespace('MAPI').Folders['x@outlook.at']
dest_folder = root_folder.Folders["Posteingang"]

move = oRule.Actions.MoveToFolder
move.__MoveOrCopyRuleAction__com__set_Enabled(True)
move.__MoveOrCopyRuleAction__com__set_Folder(dest_folder)

rules.Save()

我无法使用 pywin32 使其工作。当我运行以下代码时,出现错误并且无法创建规则:

import win32com.client as win32

o = win32.Dispatch("Outlook.Application")

caiok = o.GetNamespace("MAPI").Folders['x@outlook.at']

dest_folder = caiok.Folders["Posteingang"]
colRules = o.Session.DefaultStore.GetRules()
oRule = colRules.Create("New Rule10", 0)

oFromCondition = oRule.Conditions.From
oFromCondition.Enabled = True
oFromCondition.Recipients.Add("john@email.com")
oFromCondition.Recipients.ResolveAll

oMoveRuleAction = oRule.Actions.MoveToFolder
oMoveRuleAction.Enabled=True
oMoveRuleAction.Folder=dest_folder

colRules.Save()

错误:

Traceback (most recent call last):
  File "D:/wwo/scrapbookIII.py", line 20, in <module>
    colRules.Save()
  File "<COMObject GetRules>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (4096, 'Microsoft Outlook', 'Mindestens eine Regel kann aufgrund von ungültigen Aktionen oder Bedingungen nicht gespeichert werden.', None, 0, -2147467259), None)

我做错了什么?

【问题讨论】:

    标签: python outlook com pywin32 comtypes


    【解决方案1】:

    Python 需要括号来调用函数。所以,这什么都不做:

    oFromCondition.Recipients.ResolveAll
    

    你需要:

    oFromCondition.Recipients.ResolveAll()
    

    【讨论】:

    • 嗨蒂姆!感谢您的输入,但在工作示例中我不需要括号。
    猜你喜欢
    • 2019-04-14
    • 2014-01-13
    • 2020-08-15
    • 2018-02-19
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多