2

I have a script that checks my Outlook folder. The inconvenience is that my Outlook can either be already opened, or if not, the script will open Outlook in the background for me. I wanted to streamline it so that if my Outlook is already opened, keep it. If it was dispatched by the script, quit outlook afterwards.

So I have my script like this:

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

The dispatch differentiation works. I've looked around and found some of these answers:

COM: excelApplication.Application.Quit() preserves the process

Can't close Excel completely using win32com on Python

And tried my hands on these quit conditions, but they don't quit Outlook properly:

# 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()

This method seems to work for Excel based on the questions I've linked. Is there something different about Outlook that makes it stay open on background? I know for sure the condition is met because of print('quit'), and no errors were encountered.

I also saw this: Check with Python if Outlook is already open, if not open it, but I'd rather not import another module just for the sole purpose of closing Outlook. I want to know if there is an innate function within win32com that quits Outlook properly because it seems awkward that it doesn't.

r.ook
  • 13,466
  • 2
  • 22
  • 39
  • Perhaps it is another reason not to end. [Outlook process not ending when exiting Outlook](https://social.msdn.microsoft.com/Forums/en-US/6c152279-8286-4fdb-bfc2-6e5e83e07157/outlook-process-not-ending-when-exiting-outlook?forum=innovateonoffice), [Outlook doesn’t close](https://www.howto-outlook.com/faq/outlookdoesntclose.htm), [OUTLOOK.EXE continues running after you exit Outlook](https://www.slipstick.com/problems/outlook-exe-continues-running-after-you-exit-outlook/) – kunif Jan 29 '19 at 23:37

1 Answers1

1

I hit the same problem and I simply kill the outlook process.

import os

# Kill outlook and allow programmatic access (the script has to run as Administrator to edit the Windows Registry)
os.system('taskkill /im outlook.exe /f')
os.system('reg add HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Security\\ /v ObjectModelGuard /t REG_DWORD /d 2 /f')

# ... use outlook

# Kill the outlook again and restore the warn on programmatic access setting
os.system('taskkill /im outlook.exe /f')
os.system('reg add HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Security\\ /v ObjectModelGuard /t REG_DWORD /d 0 /f')

# Finally, restart outlook
os.system('start outlook')

xorcus
  • 999
  • 1
  • 11
  • 12