Script execution after receiving email / outlook / VBA

95 Views Asked by At
Sub WorkWithEmails(Item As Outlook.MailItem)

Dim oLookItem As Object
Dim oLookMail As MailItem
Dim oLookFldr As Folder
Dim oLookName As NameSpace
Dim Ret_Val

Set objShell = VBA.CreateObject("Wscript.Shell")
Set oLookName = Application.GetNamespace("MAPI")
Set oLookFldr = oLookName.GetDefaultFolder(olFolderInbox)

For Each oLookItem In oLookFldr.Items
    If TypeOf oLookItem Is MailItem Then
        Debug.Print oLookItem.Subject
        'Debug.Print oLookMail.ReceivedTime
        'Debug.Print oLookMail.Sender
        'Debug.Print oLookMail.SenderEmailAddress
        Debug.Print oLookItem.Body
        Ret_Val = Shell("path\Python37\python.exe " & "path\draft.py" & " -t " & oLookItem.Subject & " -d " & oLookItem.Body)
    End If
Next

End Sub

This code terminates with an error, because if I understood correctly, the rule is triggered earlier (event) than the letter appears in the folder

I tried:

Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Sub WorkWithEmails(Item As Outlook.MailItem)

Sleep (5000)
...

And

...
Sleep (5000)
For Each oLookItem In oLookFldr.Items

in this case, nothing happens when a new letter is received.

If you run, Sub WorkWithEmails(), then you work out everything perfectly (provided that the letter is in the folder).

draft.py

from datetime import datetime
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-t")
parser.add_argument("-d")
args = parser.parse_args()

f = open('path/logs/jira.log', 'a')
f.write(str(datetime.now().strftime('%d%m%y')) + ':' + args.t + ', ' + args.d + '\n')
f.close()

What am I missing?

0

There are 0 best solutions below