I'd like to create a Microsoft Outlook Macro in Python using win32com library. I looked for this in Internet, but what I found is just something related to Excel and not Outlook, for example:
They suggested me to open Outlook:
outlook = win32com.client.Dispatch("Outlook.Application")
ns = outlook.GetNamespace("MAPI")
ns.Logon(PROFILENAME)
But, then, when I should use the function to create the Macro, I don't know which object is necessary. My idea was:
- in Excel Macro tree you can see that a Macro can be created in the Worksheet, so in Python I call the ActiveSheet.
- in Outlook Macro tree, instead, you can see that a Macro is created in the Session, so in Python I should call the Session.
So I tried:
xlmodule = outlook.Session.VBProject.VBComponents.Add(1)
But I got this error:
Traceback (most recent call last):
File "C:/Users/cleo/PycharmProjects/SendAppointments/vba_script.py", line 21, in <module>
xlmodule = outlook.Session.VBProject.VBComponents.Add(1)
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.VBProject