finman69
finman69

Reputation: 311

Error Calling Python Code from VBA in Outlook

I am trying to run a python script that is triggered by a rule in outlook. My current process is to write a VBA script that is triggered by the rule - the script simply just calls the shell method as so:

Shell (".../Anaconda3/python.exe" & ".../test_email.py")

However, when I call the script in VBA I receive the error message "Compile Error: Invalid outside procedure" and the reference to my python exe path is highlighted. Does anyone know how I can resolve this issue? I've added what I have in the VBA portion with the actual error


enter image description here


When I put the code in a sub, I am not able to call the actual code from the rules manager. enter image description here

Upvotes: 0

Views: 82

Answers (3)

finman69
finman69

Reputation: 311

The two answers above are great. However, I ran into the issue of not being able to track potential errors in my script, so I ultimately ended up creating a .bat file that referenced the python executable and script and created an error logger in the bat.

Public Sub PyExecSub(mail As MailItem)
    Shell "...\Desktop\test_email.bat"
End Sub

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

You must define a sub which has the following signature:

Public Sub Test(mail as MailItem)
   Shell (".../Anaconda3/python.exe" & ".../test_email.py")
End Sub

Then Outlook will be able to recognize the method to run for a rule.

See Outlook's Rules and Alerts: Run a Script for more information.

Upvotes: 2

Allan Elder
Allan Elder

Reputation: 4094

I think you need to put the code inside a Sub, and then call the sub - not just launch it from the editor.

Sub PyExecSub()
    Shell (".../Anaconda3/python.exe" & ".../test_email.py")
End Sub

Upvotes: 0

Related Questions