【发布时间】:2019-06-22 19:01:02
【问题描述】:
我有一个检查我的 Outlook 文件夹的脚本。不便之处在于我的 Outlook 可能已经打开,或者如果没有,脚本会在后台为我打开 Outlook。我想简化它,以便如果我的 Outlook 已经打开,请保留它。如果是脚本派发的,之后退出outlook。
所以我的脚本是这样的:
from win32com.client import Dispatch, GetActiveObject
class Outlook:
def __init__(self):
self.app_str = "Outlook.Application"
try:
self.app = GetActiveObject(self.app_str)
self.dispatched = False
except: # I know I should catch the specific error, but let's neglect it for this MCVE
self.app = Dispatch(self.app_str)
self.dispatched = True
调度差异化有效。我环顾四周,找到了一些答案:
COM: excelApplication.Application.Quit() preserves the process
Can't close Excel completely using win32com on Python
并尝试了这些退出条件,但它们没有正确退出 Outlook:
# attempt 1
def quit(self):
print(self.dispatched, self.app)
if self.dispatched and not self.app is None:
print('quit!')
self.app.Application.Quit()
# I've also tried self.app.Quit()
# attempt 2
import pythoncom
def __init__(self):
...
except:
pythoncom.CoInitialize()
self.app = Dispatch(self.app_str)
self.dispatched = True
def quit(self):
print(self.dispatched, self.app)
if self.dispatched and not self.app is None:
print('quit!')
self.app.Application.Quit()
pythoncom.CoUninitialize()
根据我链接的问题,此方法似乎适用于 Excel。 Outlook 有什么不同之处使其在后台保持打开状态吗?由于print('quit'),我确定满足条件,没有遇到任何错误。
我也看到了这个:Check with Python if Outlook is already open, if not open it,但我不想仅仅为了关闭 Outlook 而导入另一个模块。我想知道win32com 中是否有一个固有函数可以正确退出 Outlook,因为它看起来很尴尬没有。
【问题讨论】:
标签: python outlook com win32com