none
Vb and search in outlook RRS feed

  • Question

  • Hi all,

    Using Office 2010.

    I have a form in access containing contact information of clients.
    A button next to the e-mail of the client OPENS OUTLOOK and applies a filter to display only the e-mails from that client in OUTLOOK.

    Private Sub filterOutlook_Click()
       'bit of code here which checks if outlook is open, if not then
       Shell Outlook.exe /cleanviews
    
       'email of contact field(hyperlink) is retreived
       contactname = Left$([E-mail contact].Value, InStr([E-mail contact].Value, "#") - 1)
    
       filtermail contactname
    End Sub

    The filtermail function is as follows:

    Public Sub filtermail(name As String)
    Dim myOlapp As Outlook.Application
    Dim objView As View
    Set objView = myOlapp.ActiveExplorer.CurrentView
    
        If LenB(name) = 0 Then
            objView.Filter = vbNullString
        Else
            objView.Filter = _ 
            "http://schemas.microsoft.com/mapi/proptag/0x0065001f CI_STARTSWITH '" & name & "'"
        End If
    
    objView.Save
    objView.Apply
    
    End Sub

    The code works fine, but only applies the filter to the folder currently open in OUTLOOK.

    If you have multiple accounts you would have to first open the inbox or sent folder and then apply the filter. Rather cumbersome, but the view filter can't be applied to all folders in one shot, see here(no explanation given btw).

    I would like to replace the code above to one where I can control the searchbar in outlook.

    So selecting "All Mail Items" or "All Outlook Items", ranking the result by account and using the following queries "system.structuredquery.virtual.from: (name)" and "to: (name)".

    Using the searchbar manually gives me the result I want. I can see all the history with a client across different accounts and folders. But having it automated would be great.

    Is it possible and how can I achieve this?

    In summation: when button clicked next to e-mail, open outlook and show messages from and to that e-mail.

    Thanks



    • Edited by Tarint Sunday, April 15, 2012 1:00 PM
    Sunday, April 15, 2012 12:55 PM

Answers

  • Solution GET!!!

    Found it:

    Public Sub filtermail(name As String)
    
    Dim myOlApp As New outlook.Application
    
        If LenB(name) = 0 Then
            myOlApp.ActiveExplorer.ClearSearch
        Else
            myOlApp.ActiveExplorer.Search "from:(" & name & ") OR to:(" & name & ")", olSearchScopeAllFolders
        End If
    
    Set myOlApp = Nothing
    End Sub



    Needs a bit of testing, but works for now.
    To call the function/sub see code posted in 1st post.

    If anyone has suggestions to improve or perhaps there is something missing, let me know.

    Cheers

    • Marked as answer by Tarint Sunday, April 15, 2012 9:08 PM
    • Edited by Tarint Sunday, April 15, 2012 9:10 PM
    Sunday, April 15, 2012 9:06 PM

All replies

  • Solution GET!!!

    Found it:

    Public Sub filtermail(name As String)
    
    Dim myOlApp As New outlook.Application
    
        If LenB(name) = 0 Then
            myOlApp.ActiveExplorer.ClearSearch
        Else
            myOlApp.ActiveExplorer.Search "from:(" & name & ") OR to:(" & name & ")", olSearchScopeAllFolders
        End If
    
    Set myOlApp = Nothing
    End Sub



    Needs a bit of testing, but works for now.
    To call the function/sub see code posted in 1st post.

    If anyone has suggestions to improve or perhaps there is something missing, let me know.

    Cheers

    • Marked as answer by Tarint Sunday, April 15, 2012 9:08 PM
    • Edited by Tarint Sunday, April 15, 2012 9:10 PM
    Sunday, April 15, 2012 9:06 PM
  • Hi Tarint,

    Thanks for posting in the MSDN Forum.

    I'm glad to hear you have solved your issue. Thanks for sharing you solution here, it's very beneficial for other community members who have similar question to see how you solved your issue.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 3:57 AM
    Moderator