0

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:

  1. Use Python to Inject Macros into Spreadsheets
  2. Write and execute excel VB macro with win32com.client

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
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
CunivL
  • 174
  • 12
  • what is your goal? – Rahul Jun 09 '17 at 11:04
  • Looks like `Session` is Null or something. – ivan_pozdeev Jun 09 '17 at 11:09
  • Related: https://stackoverflow.com/questions/17197259/use-python-to-inject-macros-into-spreadsheets maybe there's a security roadblock here, too. – ivan_pozdeev Jun 09 '17 at 11:12
  • Also, did you check [Outlook's object model](https://msdn.microsoft.com/en-us/library/ms268893.aspx) - if this is done the same way as in Excel? (That doc is for .Net. For COM, you need to drop "T:Microsoft.Office.Interop" prefix from everything.) – ivan_pozdeev Jun 09 '17 at 11:16
  • 2
    No Outlook dosen't support changing the code in the editor dynamically. There is no `VBE` object nor is there a `VBProject` or `VBComponents`. Yes you can do that with Excel but not with Outlook I'm pretty sure. Security restrictions in Outlook are much tighter than in Excel. – Pᴇʜ Jun 09 '17 at 11:19
  • @Scripting.FileSystemObject want to set a Macro to accept automatically event I am invited to. – CunivL Jun 09 '17 at 11:39
  • @ivan_pozdeev Thank you for the link, but it's about Excel. I can't understand why `Session` could be Null. – CunivL Jun 09 '17 at 11:41
  • @Peh Thank you for your advice. So won't I be allowed to change Macro dinamically in Outlook even if I change Security Options? – CunivL Jun 09 '17 at 11:44
  • @ChiaraL Yes that's what I said. May I ask why do you need to change the macro dynamically? I mean usually you code a macro and then it is finished when it works and there should be no need for dynamic changes. – Pᴇʜ Jun 09 '17 at 11:51
  • @Peh I need a Macro to accept automatically event I am invited to with a Python Script. – CunivL Jun 09 '17 at 11:58
  • @ChiaraL probably there is another approach to achieve what you want. But I still don't understand your goal if you only write one sentence, that's too less information. You should ask that as a detailed question ([edit]). Describe your situation as detailed as necessary so we understand what you are trying to do. The better your question the easier you get a good answer. – Pᴇʜ Jun 09 '17 at 12:34

0 Answers0