Hi Boardies,
I have a workbook that sends emails. I'm trying to create a log table to record if any emails weren't sent.
The problem that I'm encountering is that the code runs too quickly for the Outlook App.
I'm using Outlook 2016 in Win10 and Outlook is connected via Microsoft Exchange to Hotmail.
This is what I'm using to check Draft (olDefaultFolder 16)/Outbox (olDefaultFolder 4)/Sent (olDefaultFolder 5) folders :
And the CheckSent Function is:
At the moment this is just looking for mailitems where attachments have a certain file name, but once working I'd like to include recipient email also.
If I step through the code slowly this function correctly returns mailitems found in the sent folder. But if run normally it fails, now whether that's because it's too fast or I have the wrong code for the draft and outbox folders I do not know.
/Comfy
I have a workbook that sends emails. I'm trying to create a log table to record if any emails weren't sent.
The problem that I'm encountering is that the code runs too quickly for the Outlook App.
I'm using Outlook 2016 in Win10 and Outlook is connected via Microsoft Exchange to Hotmail.
This is what I'm using to check Draft (olDefaultFolder 16)/Outbox (olDefaultFolder 4)/Sent (olDefaultFolder 5) folders :
Code:
If CheckSent(oApp, 16, lRow.Range(1, LO1Col3Index), lRow.Range(1, LO1Col2Index) & fExt) Or CheckSent(oApp, 4, lRow.Range(1, LO1Col3Index), lRow.Range(1, LO1Col2Index) & fExt) Or CheckSent(oApp, 5, lRow.Range(1, LO1Col3Index), lRow.Range(1, LO1Col2Index) & fExt) Then
Call AddLogRow(LogLO, Now(), "Email Success", lRow.Range(1, LO1Col2Index) & fExt & " Succesfully emailed")
Else
Call AddLogRow(LogLO, Now(), "Email Fail", "Could not find " & lRow.Range(1, LO1Col2Index) & fExt & " in sent items.")
End If
And the CheckSent Function is:
Code:
Function CheckSent(oApp As Object, fIndex As Integer, Recip As String, attachname As String) As Boolean
Dim oFolder As Object 'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/folders-object-outlook
Dim oItems As Object
Dim fFilter As String
Dim mItems As Object
Set oFolder = oApp.GetNamespace("MAPI").GetDefaultFolder(fIndex)
'fFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:attachmentfilename" & Chr(34) & " LIKE '%" & attachname & "%'"
fFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/proptag/0x3704001E" _
& Chr(34) & " ci_phrasematch " & "'" & attachname & "'"
Set mItems = oFolder.Items.Restrict(fFilter)
If mItems.Count = 0 Then
CheckSent = False
Else
CheckSent = True
End If
End Function
At the moment this is just looking for mailitems where attachments have a certain file name, but once working I'd like to include recipient email also.
If I step through the code slowly this function correctly returns mailitems found in the sent folder. But if run normally it fails, now whether that's because it's too fast or I have the wrong code for the draft and outbox folders I do not know.
/Comfy