如何退出win32com上的Outlook?

2024-09-25 06:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个检查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:

^{pr2}$

基于我所链接的问题,这个方法似乎对Excel有效。Outlook是否有什么不同之处使其在后台保持开放?因为print('quit'),我确信满足了条件,并且没有遇到任何错误。在

我也看到了这个:Check with Python if Outlook is already open, if not open it,但我不想仅仅为了关闭Outlook而导入另一个模块。我想知道在win32com中是否有一个天生的功能可以恰当地退出Outlook,因为它看起来很尴尬,因为它没有。在


Tags: theself脚本appapplicationit调度excel