Home > database >  Error Calling Python Code from VBA in Outlook
Error Calling Python Code from VBA in Outlook

Time:10-07

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related