Opening Outlook with Python

2020-07-16 06:38发布

问题:

I am trying to open Outlook with Python and I got this...

import win32com.client
ol = win32com.client.Dispatch("outlook.Application")

It opens Outlook but my probles is it opens it in the notification area of Windows (where the clock is) and it doesn't open it up on the screen. I tried a ...

OL.visible= True

But I get an error that Outlook application can't be set to visible. So my question is how can I bring Outlook for, or maximze it to a full screen?

回答1:

Ok I found this to work as simple as it was....

import os
os.startfile("outlook")

It makes it into a large window and mot a minimized one.



回答2:

Have you seen this SO question? The solution there was to start up Outlook from a shell command and then attach to the running process. It seems dirty, but I've done the same with other programs out of desperation. I would expect MS Office to play nicer with COM.

I wonder if you could do something similar to the MS example here.

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myFolder= _
    myNameSpace.GetDefaultFolder(olFolderInbox)
myFolder.Display

I don't have Outlook installed so I can't test anything.



回答3:

You could also check if Outlook is running using psutil

import psutil

def is_outlook_running():
    for p in psutil.process_iter(attrs=['pid', 'name']):
        if "OUTLOOK.EXE" in p.info['name']:
            print("Yes", p.info['name'], "is running")
            break
    else:
        print("No, Outlook is not running")
        os.startfile("outlook")
        print("Outlook is starting now...")