Can I add a button in an Access stock program to search Outlook?
Is there a way I can make a button on an access database that automatically searches Outlook? My idea is to have a button besides the email address which ca开发者_开发知识库n be clicked and it will open Outlook, or jump to Outlook if its open, and search all items for the customer's email address?
Below is a VBA sub that takes a search string and searches in either the existing instance of Outlook or creates a new instance to search. Tested in Office 2010. It's worth putting in a real error handler if this is going to be used by others.
You'll need to make a reference to "Microsoft Outlook 14.0 Object Library" or whatever version you have. You can do this in the VBA window via Tools->References.
If you're feeling fancy you can display the search results in Access itself with the help of the AdvancedSearch method.
Sub outlookSearch(searchString As String)
Dim app As Outlook.Application
'This will throw an error if there's no instances of Outlook running
' so resume after the error.
On Error Resume Next
Set app = GetObject(, "Outlook.Application")
On Error GoTo 0 'Replace this with a real error handler
'If the app variable is empty
If app Is Nothing Then
'Create a new instanc eof outlook
Set app = CreateObject("Outlook.Application")
'Add an explorer showing the inbox
app.Explorers.Add app.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'Make the explorer visible
app.Explorers(1).Activate
End If
'Search all folders for searchString
app.ActiveExplorer.search searchString, olSearchScopeAllFolders
Set app = Nothing
End Sub
精彩评论