Reputation: 125
I'm trying to trigger a Python script when an email is moved to a certain folder.
The idea is to set up a rule in Outlook that moves an email to a specific folder when it contains certain keywords, then have a VBA script launch the Python script with arguments when a new item is added to a folder. I am following this guide.
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items
Private Sub Application_Startup()
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")
Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objItems = objWatchFolder.Items
Set objWatchFolder = Nothing
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
MsgBox "Message subject: " & Item.Subject & vbcrlf & "Message sender: " & Item.SenderName &" (" & Item.SenderEmailAddress & ")"
Dim subject As Item.subject
Shell("""C:\Path\to\python.exe"" ""C:\Path\With Spaces\script.py """ & subject)
Set Item = Nothing
End Sub
Previously, the following worked after declaring the subject variable using Dim subject As Item.subject
:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py """ & subject)
Now I am getting a compile error
user-defined type not defined.
This error occurs on the Dim subject As Item.subject
line.
When I try to pass the email's subject to the Python script via:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py """ & Item.subject)
It does not crash, but it looks like the Python script never runs.
When I do:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py "" & Item.subject")
It passes &
and Item.subject
as string arguments - the email's subject stored in Item.subject is not passed, but the literal string "Item.subject".
Why would Dim subject As Item.subject
now cause an error and how do I pass the email's subject as an argument to my Python script?
Upvotes: 0
Views: 572
Reputation: 3634
I would assume your issue is with incorrect double quote assignments and spacing for calling the python code. I would also assume the subject can have spaces in it so should be enclosed in double quotes which therefore gives rise to (assuming I have the nomenclature correct for the call):
"""C:\Path\to\python.exe"" ""C:\Path\With Spaces\script.py"" """ & Item.subject & """"
You can use the Debug.Print to check the string is as expected before the call to run the python script. Disclaimer: I haven't used python scripts so this is untested
Option Explicit
Private WithEvents objItems As Outlook.Items
Private Sub Application_Startup()
With Application.Outlook
Set ObjItems = .GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End With
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
If TypeName(Item) = "MailItem" Then
Dim strFunc As String: strFunc = """C:\Path\to\python.exe"" ""C:\Path\With Spaces\script.py"" """ & Item.subject & """"
Debug.Print strFunc
Shell strFunc, vbMinimizedNoFocus
End If
End Sub
Upvotes: 1