Results 1 to 7 of 7

Thread: program to search through an outlook mailbox by Name or Subject Field

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Question 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

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    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
    Last edited by bggrif; Nov 25th, 2017 at 07:27 AM.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    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 View Post
    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    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 View Post
    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
    Last edited by bggrif; Nov 26th, 2017 at 05:02 AM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width