Reputation: 311
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
When I put the code in a sub, I am not able to call the actual code from the rules manager.
Upvotes: 0
Views: 82
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
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
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