0

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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
finman69
  • 309
  • 1
  • 8

3 Answers3

2

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

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
Allan Elder
  • 4,052
  • 17
  • 19
  • The problem with putting it in a sub then becomes getting the Outlook Rule manager to recognize the script - I can't seem to find a way to call this sub or the script the sub is written in so that the sub can be triggered by the rule. This is a good idea, though. – finman69 Oct 06 '22 at 19:26
0

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
finman69
  • 309
  • 1
  • 8