-
Mar 24th, 2023, 11:13 AM
#1
Thread Starter
Member
search for emails in sent emails
Hi everyone, I'm here again. With another problem(at least for me).
I should check if I actually sent an email, and this works. But I noticed that if I have a lot of emails in the sent folder of outlook vs generate an error.
For me it would be enough to do a search in the emails sent during the day.
Code:
Fldr = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
i = 1
For Each oMsg In Fldr.Items()
If oMsg.HTMLBody.Contains(ecnf) Then GoTo 003 Else GoTo 001
001:
Next
MsgBox(“Email non ok”)
GoTo 002
003:
i = i + 1
If oMsg.HTMLBody.Contains(ecnf) = True Then
inv = False
MsgBox(“Email ok”)
GoTo 002
'Else
'GoTo 001
End If
002:
'other irrelevant code
This is a error at line
For Each oMsg In Fldr.Items()

If I try to delete very old emails the code works perfectly. I need to delete last month's emails and older emails but I'd like to avoid that if possible.
-
Mar 25th, 2023, 07:18 AM
#2
Re: search for emails in sent emails
I doubt if this Line would work
Code:
'...
If oMsg.HTMLBody.Contains(ecnf) Then
'....
should be more like
Code:
If oMsg.HTMLBody.Contains("ecnf") Then
so what is ecnf ?
try and adapt this code for your needs
Code:
Option Strict On
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objOutlook As Outlook.Application = New Outlook.Application()
'NameSpace,Logon
Dim objOutlookoNS As Outlook.NameSpace = objOutlook.GetNamespace("mapi")
objOutlookoNS.Logon("Outlook", Reflection.Missing.Value, False, True)
Dim outLookSentmails As Outlook.MAPIFolder = objOutlookoNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim outLookItems As Outlook.Items = outLookSentmails.Items
'Datatable
Dim dtSentmails As New DataTable
dtSentmails.Columns.Add("Sent To")
dtSentmails.Columns.Add("Topic")
dtSentmails.Columns.Add("Received")
'search
Dim drSentmails As DataRow
For i As Integer = 1 To outLookSentmails.Items.Count
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
If myMails.UnRead Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
End If
Next
'add to DGV
DataGridView1.DataSource = dtSentmails
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 27th, 2023, 06:59 AM
#3
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
I doubt if this Line would work
Code:
'...
If oMsg.HTMLBody.Contains(ecnf) Then
'....
should be more like
Code:
If oMsg.HTMLBody.Contains("ecnf") Then
so what is ecnf ?
try and adapt this code for your needs
Code:
Option Strict On
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objOutlook As Outlook.Application = New Outlook.Application()
'NameSpace,Logon
Dim objOutlookoNS As Outlook.NameSpace = objOutlook.GetNamespace("mapi")
objOutlookoNS.Logon("Outlook", Reflection.Missing.Value, False, True)
Dim outLookSentmails As Outlook.MAPIFolder = objOutlookoNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim outLookItems As Outlook.Items = outLookSentmails.Items
'Datatable
Dim dtSentmails As New DataTable
dtSentmails.Columns.Add("Sent To")
dtSentmails.Columns.Add("Topic")
dtSentmails.Columns.Add("Received")
'search
Dim drSentmails As DataRow
For i As Integer = 1 To outLookSentmails.Items.Count
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
If myMails.UnRead Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
End If
Next
'add to DGV
DataGridView1.DataSource = dtSentmails
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
End Sub
Thank you for the answer, now I try to adapt your code.
Ecnf is a string type variable where I check if the ecnf text is contained in the sent emails, so I know that the email was sent with the ecnf date and time
-
Mar 28th, 2023, 04:36 AM
#4
Thread Starter
Member
Re: search for emails in sent emails
Hello everyone, I couldn't solve my problem. If I have a lot of emails in sent email, it doesn't know how much to quantify, but if I move last month's emails to the less recent ones, then the cycle works perfectly, otherwise it produces this error
Attachment 187272
-
Mar 28th, 2023, 06:28 AM
#5
Thread Starter
Member
Re: search for emails in sent emails
I think I understand but I don't know how to solve that the program crashes if there are also responses to meetings for example
-
Mar 28th, 2023, 06:49 AM
#6
Re: search for emails in sent emails
you have to explain better what you want to filter in sentmails
this here would filter the emails 5 Days old
Code:
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Dim objOutlook As Outlook.Application = New Outlook.Application()
'NameSpace,Logon
Dim objOutlookoNS As Outlook.NameSpace = objOutlook.GetNamespace("mapi")
objOutlookoNS.Logon("Outlook", Reflection.Missing.Value, False, True)
Dim outLookSentmails As Outlook.MAPIFolder = objOutlookoNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim outLookItems As Outlook.Items = outLookSentmails.Items
'Datatable
Dim dtSentmails As New DataTable
dtSentmails.Columns.Add("Sent To")
dtSentmails.Columns.Add("Topic")
dtSentmails.Columns.Add("Received")
'search
Dim drSentmails As DataRow
For i As Integer = 1 To outLookSentmails.Items.Count
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
'If myMails.UnRead Then
If myMails.ReceivedTime.Date = Date.Today.AddDays(-5) Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
End If
Next
'add to DGV
DataGridView1.DataSource = dtSentmails
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 08:02 AM
#7
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
you have to explain better what you want to filter in sentmails
this here would filter the emails 5 Days old
Code:
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Dim objOutlook As Outlook.Application = New Outlook.Application()
'NameSpace,Logon
Dim objOutlookoNS As Outlook.NameSpace = objOutlook.GetNamespace("mapi")
objOutlookoNS.Logon("Outlook", Reflection.Missing.Value, False, True)
Dim outLookSentmails As Outlook.MAPIFolder = objOutlookoNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim outLookItems As Outlook.Items = outLookSentmails.Items
'Datatable
Dim dtSentmails As New DataTable
dtSentmails.Columns.Add("Sent To")
dtSentmails.Columns.Add("Topic")
dtSentmails.Columns.Add("Received")
'search
Dim drSentmails As DataRow
For i As Integer = 1 To outLookSentmails.Items.Count
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
'If myMails.UnRead Then
If myMails.ReceivedTime.Date = Date.Today.AddDays(-5) Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
End If
Next
'add to DGV
DataGridView1.DataSource = dtSentmails
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
End Sub
Thank you very much for the answer.
I should only know if in the emails sent during the day the ecnf string is present in the body of the email. So I know that user sent the email.
Now with this code I'm at a good point for what I had to do, I only filter those sent today.
But it crashes when for example I reply to a meeting request or calendar event. Is there a way to exclude them or, if present, pass them on?
Thanks again so much.
Code:
strB.AppendLine = html table
oMsg.HTMLBody = strB.ToString
Dim olNS As Outlook.[NameSpace] = CType(oApp.GetNamespace("MAPI"), Outlook.[NameSpace])
Dim oMsg As Outlook.MailItem
Dim d = Date.Now.ToString("dddd dd/MM/yyyy" & " hours" & " HH:mm:ss")
Dim Fldr As MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim Mgok As Outlook.Items = Fldr.Items.Restrict("@SQL=%today(""urn:schemas:httpmail:datereceived"")%")
ecnf = Report.Item(0, 0).Value & " - " & Report.Item(1, 0).Value & " - " & d
Try
For Each oMsg In Mgok
If oMsg.HTMLBody.Contains(ecnf) Then GoTo send Else GoTo 001
001:
next
-
Mar 28th, 2023, 08:13 AM
#8
Re: search for emails in sent emails
just extend the filter...
ReceivedTime = 5 Days old
Body = search for Barbara
Code:
'....
If myMails.ReceivedTime.Date >= Date.Today.AddDays(-5) AndAlso myMails.Body.Contains("Barbara") Then
'
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 08:31 AM
#9
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
just extend the filter...
ReceivedTime = 5 Days old
Body = search for Barbara
Code:
'....
If myMails.ReceivedTime.Date >= Date.Today.AddDays(-5) AndAlso myMails.Body.Contains("Barbara") Then
'
the problem is that if in the emails of those 5 days I reply accept or not to a request that is sent to me, when the program browses all the emails, when I get to that one it stops and gives me an error. Is there a way to exclude them?
Attachment 187274
-
Mar 28th, 2023, 08:36 AM
#10
Re: search for emails in sent emails
well the code you use in Post#7 looks strange
why would you want to use Goto in .NET
did you try my Code from Post#6 ? I mean without any changes
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 08:55 AM
#11
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
well the code you use in Post#7 looks strange
why would you want to use Goto in .NET
did you try my Code from Post#6 ? I mean without any changes
I make the program do different things depending on what the users are doing.
I'm trying to fit it.
what maybe i look for something like outlook rule maybe (restrict items whether it is an update or a meeting attendance)
-
Mar 28th, 2023, 08:59 AM
#12
Re: search for emails in sent emails
did you try this Code with no changes
Code:
Option Strict On
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objOutlook As Outlook.Application = New Outlook.Application()
'NameSpace,Logon
Dim objOutlookoNS As Outlook.NameSpace = objOutlook.GetNamespace("mapi")
objOutlookoNS.Logon("Outlook", Reflection.Missing.Value, False, True)
Dim outLookSentmails As Outlook.MAPIFolder = objOutlookoNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
Dim outLookItems As Outlook.Items = outLookSentmails.Items
'Datatable
Dim dtSentmails As New DataTable
dtSentmails.Columns.Add("Sent To")
dtSentmails.Columns.Add("Topic")
dtSentmails.Columns.Add("Received")
'search
Dim drSentmails As DataRow
For i As Integer = 1 To outLookSentmails.Items.Count
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
' End If
Next
'add to DGV
DataGridView1.DataSource = dtSentmails
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 09:11 AM
#13
Thread Starter
Member
Re: search for emails in sent emails
i am trying and i have an error here
Code:
......
Dim dtSentmails As New DataTable
......
'New' cannot be used on an interface
-
Mar 28th, 2023, 09:14 AM
#14
Thread Starter
Member
Re: search for emails in sent emails
now i'm ok whit your code but the error is.
Attachment 187275
-
Mar 28th, 2023, 09:29 AM
#15
Re: search for emails in sent emails
sorry I can't see or read the Attachment
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 09:38 AM
#16
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
sorry I can't see or read the Attachment
Code:
......
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
.......
this error:
System.InvalidCastException: 'Impossibile eseguire il cast di oggetti COM di tipo 'System.__ComObject' in tipi di interfaccia 'Microsoft.Office.Interop.Outlook.MailItem'. L'operazione non č stata completata perché la chiamata QueryInterface sul componente COM per l'interfaccia con IID '{00063034-0000-0000-C000-000000000046}' non č riuscita a causa del seguente errore: Interfaccia non supportata. (Eccezione da HRESULT: 0x80004002 (E_NOINTERFACE)).'
-
Mar 28th, 2023, 09:42 AM
#17
Re: search for emails in sent emails
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 10:08 AM
#18
Thread Starter
Member
Re: search for emails in sent emails
System.InvalidCastException: 'Unable to cast COM objects of type 'System.__ComObject' to interface types 'Microsoft.Office.Interop.Outlook.MailItem'. The operation failed because the QueryInterface call on the COM components for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed with the following error: Interface not supported. (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
-
Mar 28th, 2023, 10:09 AM
#19
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
sorry English or German
System.InvalidCastException: 'Unable to cast COM objects of type 'System.__ComObject' to interface types 'Microsoft.Office.Interop.Outlook.MailItem'. The operation failed because the QueryInterface call on the COM components for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed with the following error: Interface not supported. (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
-
Mar 28th, 2023, 10:18 AM
#20
Re: search for emails in sent emails
 Originally Posted by Marco:G
System.InvalidCastException: 'Unable to cast COM objects of type 'System.__ComObject' to interface types 'Microsoft.Office.Interop.Outlook.MailItem'. The operation failed because the QueryInterface call on the COM components for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed with the following error: Interface not supported. (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
Try this small change
Code:
For i As Integer = 1 To outLookSentmails.Items.Count
If outLookItems.Item(i).MessageClass = "IPM.Note" Then '<<<<<<<<<<<<<<<<<<<<<<<<<
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
' End If
End If '<<<<<<<<<<<<<<<<<<<<<<<<<
Next
to make sure it is a mail item.
-
Mar 28th, 2023, 10:20 AM
#21
Re: search for emails in sent emails
that is strange, which Outlook Version do you have ?
try like this
Code:
'Dim myMails = DirectCast(outLookItems.Item(i), Outlook.MailItem)
'or
Dim myMails = CType(outLookItems.Item(i), Outlook.MailItem)
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 28th, 2023, 10:30 AM
#22
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
that is strange, which Outlook Version do you have ?
try like this
Code:
'Dim myMails = DirectCast(outLookItems.Item(i), Outlook.MailItem)
'or
Dim myMails = CType(outLookItems.Item(i), Outlook.MailItem)
Office 365
-
Mar 28th, 2023, 10:39 AM
#23
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by dbasnett
Try this small change
Code:
For i As Integer = 1 To outLookSentmails.Items.Count
If outLookItems.Item(i).MessageClass = "IPM.Note" Then '<<<<<<<<<<<<<<<<<<<<<<<<<
Dim myMails = DirectCast(outLookItems.Item(i), MailItem)
'If myMails.To.Contains("Barbara") Then
drSentmails = dtSentmails.NewRow
drSentmails("Sent To") = myMails.To
drSentmails("Topic") = myMails.ConversationTopic
drSentmails("Received") = myMails.ReceivedTime
dtSentmails.Rows.Add(drSentmails)
' End If
End If '<<<<<<<<<<<<<<<<<<<<<<<<<
Next
to make sure it is a mail item.
with this code it does something, xo it copies me random mails I don't know what calculation it does, and puts them in my data gridview
-
Mar 28th, 2023, 10:40 AM
#24
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by Marco:G
Office 365
with the two codes it gives me the same error
-
Mar 28th, 2023, 10:42 AM
#25
Thread Starter
Member
Re: search for emails in sent emails
the code I put in function #7 is only when in the folder it finds a meeting response or I think similar it goes into error. with let's say normal reply emails it works perfectly
-
Mar 29th, 2023, 03:09 AM
#26
Re: search for emails in sent emails
 Originally Posted by Marco:G
with this code it does something, xo it copies me random mails I don't know what calculation it does, and puts them in my data gridview
well count the Emails in your Sentbox and compare to the Datagridview! now there should be the same Count !
this is the first step before you go into Filtering the sentBox
add this line to the Code you have...
Code:
'....
'add to DGV
DataGridView1.DataSource = dtSentmails
MessageBox.Show(CStr(dtSentmails.Rows.Count))
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
check if you now have the same amount of Emails
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 29th, 2023, 07:44 AM
#27
Thread Starter
Member
Re: search for emails in sent emails
 Originally Posted by ChrisE
well count the Emails in your Sentbox and compare to the Datagridview! now there should be the same Count !
this is the first step before you go into Filtering the sentBox
add this line to the Code you have...
Code:
'....
'add to DGV
DataGridView1.DataSource = dtSentmails
MessageBox.Show(CStr(dtSentmails.Rows.Count))
objOutlookoNS.Logoff()
objOutlookoNS = Nothing
outLookItems = Nothing
objOutlook = Nothing
check if you now have the same amount of Emails
now i try to update your code.
In the meantime, thanks for all the help.
Trying this morning I changed my oMsg variable from outlook.mailitem to object, now it seems I get this error:
could not find body member in meetingitem.
Because I use this for looping
Code:
If oMsg.HTMLBody.contains(ecnf)
where oMsg.HTMLBody = strB.ToString
strB I use it in the email as an appendline
ecnf as string
-
Mar 29th, 2023, 09:19 AM
#28
Thread Starter
Member
[RESOLVED] search for emails in sent emails
Thank you all, that's how I fixed it:
Code:
For Each oMsg In Mgok
If (TypeOf oMsg Is Outlook.MeetingItem) Then GoTo 001
If oMsg.HTMLBody.contains(ecnf) Then GoTo invia Else GoTo 001
001:
Next
I don't know if that's correct but it does what it's supposed to do for me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|