1

I need to create some sort of trigger in an outlook where a python script will be executed as new emails are received in inbox. I did refer to this link: How do I trigger a macro to run after a new mail is received in Outlook? and have written the following script:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
 default local Inbox
 Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub test_macro(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
  Set Msg = item
  Ret_Val = Shell("python <path-of-python-script>")
  Debug.Print "Value: ", Ret_Val
  If Ret_Val <> 0 Then
     MsgBox "Couldn't run python script", vbOKOnly
  End If
End If
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

Although its not giving any error but due to some reasons my python script is not executing. I've configured macro settings in outlook accordingly and also created a new rule according to documentation available. But still not able to achieve the intended result.

Any help is appreciated.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Udit Gupta
  • 15
  • 1
  • 5

1 Answers1

0

You need to rename your test_macro sub to Items_ItemAdd. There is a reason why that name is used in the answer that you copied your code from. Items_ItemAdd means that the sub is an event handler for ItemAdd event of object called Items.

Couple of notes:

  • Make sure to put your code into the ThisOutlookSession module and restart Outlook to initialize it. The code won't work in a standard module.
  • There is no need to set any rules to run this macro. It will run whenever a new item is received (or added to inbox).
  • I would suggest not to reuse class names for class instances (objects), as you do eg. in Private WithEvents Items As Outlook.Items. It is confusing.
  • To test this, just copy a an e-mail from some folder (Ctrl+C) and paste it into your Inbox (Ctrl+V). The macro should run.

Try the following code exactly as I pasted it. (I kept your confusing variable names.)

ThisOutlookSession:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    ' default local Inbox
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    MsgBox "Items_ItemAdd listener initialized."
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    On Error GoTo ErrorHandler
    Dim Msg As Outlook.MailItem
    If TypeName(Item) = "MailItem" Then
        Set Msg = Item
        MsgBox "Python script will run now."
        Ret_Val = Shell("python <path-of-python-script>")
        Debug.Print "Value: ", Ret_Val
        If Ret_Val <> 0 Then
           MsgBox "Couldn't run python script", vbOKOnly
        End If
    End If

ProgramExit:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub
Petr Srníček
  • 2,296
  • 10
  • 22