首页 > 解决方案 > Is it possible to iterate through only open/displayed Outlook emails using Python?

问题描述

I'm trying to get information into python from an email in Outlook. I'm a beginner with python -- I've done a lot with win32api and selenium, but I haven't written any scripts to work with Outlook at all yet. That being said, I originally figured this would be pretty easy and now it seems like it may not be possible.

Everything I can find about python/Outlook is about looping through the entire inbox or a designated folder (basically handling a batch of emails stored somewhere). I don't want to do that though because the email's sender, subject, body content, folder location, etc. are all variable. I just want to loop through the emails that are currently open. Ideally I will double click on an email to open it from my inbox, run the code, and have the code reference that open email.

It's very easy for me to do this successfully in VBA and I use it all the time. I iterate through all open Outlook windows until a specific phrase (unique to the email I want) is found.

VBA:

For i = 1 To outlookApp.Inspectors.Count
    Set eMail = outlookApp.Inspectors.Item(i).CurrentItem
    'work with eMail

Python:

[equivalent if possible]

If it's not possible to do this using python, is there some other way that I can specify an individual email from my inbox for the code to reference? At the end of the day, I just need to pull some text out of it. The hard part is telling python which email has the text.

标签: pythonemailoutlook

解决方案


使用什么编程语言并不重要。Python 可用于自动化 Outlook 以及其他语言。

但是,请注意,Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能在此环境中运行 Office 时表现出不稳定的行为和/或死锁。

如果您正在构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用来自服务器端解决方案的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在Office 服务器端自动化的注意事项文章中阅读有关此内容的更多信息。

一种可能的解决方法是使用 Outlook 所基于的低级 API - Extended MAPI 或围绕该 API 的任何其他第三方包装器,例如 Redemption。EWS 也是一种可行的方法。在开始在 Exchange 中使用 Web 服务一文中阅读有关此内容的更多信息。


推荐阅读