【发布时间】:2021-12-06 17:53:55
【问题描述】:
我正在使用 Python 3.x,并尝试根据主题行中的文本在 Outlook 中创建规则。到目前为止,这是我的代码:
...
o = comtypes.client.CreateObject("Outlook.Application")
rules = o.Session.DefaultStore.GetRules()
rule = rules.Create(zapNumber, 0)
condition = rule.Conditions.Subject
condition.Text = ['Foo', 'Bar']
condition.Enabled = True
...
我收到一个错误:
condition.Text = ['Foo', 'Bar']
File "******************************************************\site-packages\comtypes\__init__.py", line 284, in __setattr__
object.__setattr__(self,
_ctypes.COMError: (-2147024809, 'The parameter is incorrect.', ('Sorry, something went wrong. You may want to try again.', 'Microsoft Outlook', None, 0, None))
我不确定是否有办法设置 Text 属性以及为什么我尝试它不会采取任何措施。
【问题讨论】:
-
这是
comtypes包的链接:pythonhosted.org/comtypes/… 查看示例,它建议您尝试将数组传递为condition.Text = ('Foo','Bar')。 -
@DS_London 如果我使用
condition.Text = ('Foo','Bar'),我会得到与上述错误相同的结果 -
我用 [] 括号尝试了您的原始代码。它工作得很好,所以问题可能出在 comtypes 在幕后生成的 python 包装器中。我没有 comtypes 包,所以 pip 安装了它(Python 3.7 上的 v1.1.10):所以我所有的 python 包装器都是新生成的。也许尝试两件事:(a)升级comtypes,和/或(b)尝试清除接口缓存。如果您输入
print(comtypes.client.gen_dir)行,它将告诉您这些包装器的存储位置。删除该目录将强制 comtypes 重新创建包装器,这可能会解决您的问题。 -
@DS_London 这是我的python版本。前几天我安装了最新的 3.10,当我像你说的那样卸载并切换到 3.7 时,它运行良好。非常感谢。
标签: python python-3.x vba outlook win32com