deepImplement
deepImplement

Reputation: 13

How to start Outlook with Python and win32com

I manage to open Outlook Elements like mails or task with this code:

import win32com.client as win32

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(4)
new_mail.Display(True)

And its possible to open Excel with this code:

excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True

But how can I just open the main window of outlook? Can't find any functions or numbers for the "createItem" to just open the normal outlook window.

Someone got ideas?

I tried to search throu the MS VBA Api but couldnt find any solution

Upvotes: 1

Views: 803

Answers (3)

0m3r
0m3r

Reputation: 12499

To start Outlook application, try os.startfile("outlook") or see example

import psutil
import os


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


if __name__ == "__main__":
    outlook = Outlook()
    outlook.is_outlook_running()

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

You need to display an Explorer window of Outlook. The Explorers.Add method creates a new instance of the explorer window, then you just need to call the Explorer.Display method which displays a new Explorer object for the folder. The Display method is supported for explorer and inspector windows for the sake of backward compatibility. To activate an explorer or inspector window, use the Activate method instead. Here is the sample VBA code which opens a new explorer window for the Drafts folder in Outlook:

Sub DisplayDrafts() 
 Dim myExplorers As Outlook.Explorers 
 Dim myOlExpl As Outlook.Explorer 
 Dim myFolder As Outlook.Folder 
 
 Set myExplorers = Application.Explorers 
 Set myFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDrafts) 
 
 Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation) 
 
 myOlExpl.Display  
End Sub

The Outlook object model is common for all programming languages, so I think you will find the sequence of property and method calls that should be used to get the job done.

Upvotes: 0

BigBen
BigBen

Reputation: 49998

Inspired by this, one option might be:

outlook.Session.GetDefaultFolder(6).Display()

where 6 corresponds to the inbox as documented here.

Upvotes: 1

Related Questions