0
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?

Violet
  • 99
  • 1
  • 5
  • Possible duplicate of [How do I trigger a macro to run after a new mail is received in Outlook?](https://stackoverflow.com/questions/11263483/how-do-i-trigger-a-macro-to-run-after-a-new-mail-is-received-in-outlook) – niton Jan 16 '21 at 20:55
  • What is the exact error? – Dmitry Streblechenko Jan 16 '21 at 22:06
  • there is either no error, or "array bounds" – Violet Jan 16 '21 at 22:09
  • The ItemAdd event is triggered after the item is in a folder. "... if I understood correctly, the rule is triggered earlier (event) than the letter appears in the folder" – niton Jan 16 '21 at 22:23
  • @niton when using the code in the response from your link - `Private WithEvents Items As Outlook.Items` Compile Error: Only valid in object module – Violet Jan 18 '21 at 06:30
  • 1
    The answer is complete. If necessary there is more https://stackoverflow.com/questions/48913002/only-valid-in-object-module, https://stackoverflow.com/questions/34720620/how-to-apply-withevents-trigger and https://stackoverflow.com/questions/52494601/why-does-my-withevent-doesnt-work-in-outlook-vba – niton Jan 18 '21 at 13:35

0 Answers0