Help with vb and search in outlook

Joined
Apr 15, 2012
Messages
2
Reaction score
0
Hello,

Using Office 2010.

In access I have a form 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.

Code:
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:
Code:
Public Sub filtermail(name As String)

Dim objView As View
Set objView = outlook.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.

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


So selecting "All Mail Items" or "All Outlook Items", ranking the result by account or date 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
 
Joined
Apr 15, 2012
Messages
2
Reaction score
0
Found it:

Code:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top