0

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?

2
  • Pictures of code are not useful. Please see here: stackoverflow.com/help/how-to-ask Commented May 24, 2021 at 12:39
  • @Tragamor: Thanks, I edited the question to get rid of the screen shot. Commented May 25, 2021 at 13:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.