Search code examples
pythonoutlookpywin32win32comoffice-automation

Parsing a multiple choice parameter to an Outlook form using Python


At my company, when I send an email using outlook, after hitting 'send' an 'On Action Event' is triggered which is a pop up titled 'select form' that asks for a selection from a dropdown. I select, then click 'ok' and the email sends.

After running the below code, the 'select form' pop up appears. I would like to find a way to populate the form from within the code so that the email sends without manual intervention

import win32com.client as win32
 
olApp = win32.Dispatch('Outlook.Application')
olNS = olApp.GetNameSpace('MAPI')
 
# construct email item object
mailItem = olApp.CreateItem(0)
mailItem.Subject = 'Hello 123'
mailItem.BodyFormat = 1
mailItem.Body = 'Hello There'
mailItem.To = '<recipient email>'
mailItem.Sensitivity  = 2
# optional (account you want to use to send the email)
mailItem._oleobj_.Invoke(*(64209, 0, 8, 0, olNS.Accounts.Item('<senders email>')))
mailItem.Display()
#mailItem.Save()
mailItem.Send()

Solution

  • Sounds like an Outlook add-in or VBA macro is working in your Outlook (for displaying such dialogs) because you are setting the MailItem.SendUsingAccount property in the code correctly. You may ask developers for any public interface that can be available to developers and which you may call from your script. For example, see Walkthrough: Call code in a VSTO Add-in from VBA for more information.