program to search through an outlook mailbox by Name or Subject Field
Hi VB Forums,
thanks for all your help last time.
I find myself stuck with an issue. we are automating our processes here and as part of that i am attempting to write a small app that will search through a mailbox by name (email address) and or Subject field.
i'm not sure where to start. any suggestions?
I will be looking through the forums in the meantime but id rather ask the experts 1st.
appreciate it so much guys
Grif
Re: program to search through an outlook mailbox by Name or Subject Field
I have generated the following code as a tester. it uses a text box as the way to search for the subject
it should be using the inbox
Code:
Imports Microsoft.Office.Tools
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Public Class Form1
Private olFolderInbox As OlDefaultFolders
Private olMail As Object
Sub Search_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim Found As Boolean
Dim searchtxt As String
myNameSpace = myOlApp.GetNamespace("Inbox")
myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
myitems = myInbox.Items
Found = False
searchtxt = TextBox1.Text
For Each myitem In myitems
If myitem.Class = olMail Then
If InStr(1, myitem.Subject, searchtxt) > 0 Then
MsgBox("found email")
Found = True
End If
End If
Next myitem
'If the subject isn't found:
If Not Found Then
' NoResults.Show
End If
myOlApp.Quit
myOlApp = Nothing
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call Search_Inbox()
End Sub
End Class
Re: program to search through an outlook mailbox by Name or Subject Field
the outlook items have a restrict method that will allow you to only have items that match the restriction criteria, you can then check the count to see how many items match that criteria
Re: program to search through an outlook mailbox by Name or Subject Field
Hi Thank you for replying to this problem i have. I really appreciate your time. I'm not going to lie. I don't really understand what you mean by that. All i need it to do is locate an email in my inbox based on the email address i type into textbox1 or the subject that i type into textbox1. Can you help me with that. I would be in your debt!
Quote:
Originally Posted by
westconn1
the outlook items have a restrict method that will allow you to only have items that match the restriction criteria, you can then check the count to see how many items match that criteria
Re: program to search through an outlook mailbox by Name or Subject Field
try like this, within your existing code
Code:
Set myfolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
msgbox myfolder.Items.Count
Set g = myfolder.Items.Restrict("[subject] = '" & textbox1 & "'")
msgbox g.items.count
you can restrict on many of the message properties, including deliverytime and lastmodificationtime, see the help files for details and which properties you can not restrict on
Re: program to search through an outlook mailbox by Name or Subject Field
Westconn1 you sir are a true hero,
quick question,
I'm using visual studio 2017 so the set command generates an error, probably should have lead with that. my apologies
so when i remove the set command it goes a little weird.
also application.getnamespace throws an error saying reference to a non shared member required an object reference.
not really sure what that is
I truly appreciate your help and how knowledgeable you are
Quote:
Originally Posted by
westconn1
try like this, within your existing code
Code:
Set myfolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
msgbox myfolder.Items.Count
Set g = myfolder.Items.Restrict("[subject] = '" & textbox1 & "'")
msgbox g.items.count
you can restrict on many of the message properties, including deliverytime and lastmodificationtime, see the help files for details and which properties you can not restrict on
Re: program to search through an outlook mailbox by Name or Subject Field
probably need to define the variables, i do most of my coding in VB6 and VBA, so i do not have those problems
you may have to search like vb.net outlook items restrict, plenty of results, i am sure many with samples