Skip to content
Advertisement

Send a email already created in a window with a macro Outlook Python

I’m trying to automatize Outlook with python with win32com.client in python.

I have already a Macro that creates me a email, with all the subject and attached files.

The problem is that when I try to automatize it with Python, i don’t know how to select the window that the macro open with all the info, and put the address to whom i want to send. Example: I want to send it to “Albert” all the emails that has number “1234” in attach files.

Also, i get error " AttributeError: 'NoneType' object has no attribute 'To' "

outlook = win32.dynamic.Dispatch('Outlook.Application')

namespace = outlook.GetNameSpace('MAPI')
mail = outlook.ActiveWindow().Display()

print(type(mail))

mail. To = "Albert@gmail.com"


mail. Send()

Code from VBA is : (i deleted some details that are not important) ‘Generate MailID

strMailID = GenerateMailID

‘Generate xls file

 strFileName =Environ$("temp") & "/file directory"

‘Create mail and attach xls file

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

With OutMail

    .Subject = "DETAILS FROM EXCEL "
    .HTMLBody = "DETAIILS  "
    .Attachments.Add strFileName
    .UserProperties.Add "MailID", 1, False, 1
    .UserProperties("MailID") = strMailID
    .Display

Advertisement

Answer

Firstly, Application.ActiveWindow will return either Explorer or Inspector object, and you only want the latter. The current inspector is returned by Application.ActiveInspector.

And once you have an Inspector object, use Inspector.CurrentItem to retrieve the message being composed.

Thirdly, if you alreay have code that creates the message, why not simply call MailItem.Display to show it to the user? And if the message is shown in an inspector, why do you need to call MailItem.Send instead of letting the user click the Send button when they are ready? You really need to post your code that creates the message.

Advertisement