I haven’t posted a Lazy Programmer’s macro in a long time but the opportunity to do some lazy programming arose this week when a user wanted to replace Bing in the Smart Lookup function with Google. While this is not possible, you can use a macro to (mostly) replicate the feature using your favorite search engine.
Outlook 2016's Smart Lookup feature is locked in to using Bing as the search engine. My question is would it be possible to write macro which adds an option to the context popup menu when text in an email is selected and right-clicked, that would allow us to search with Google - even if it meant calling out to an external browser?
Is it doable? Sure. We can send the selected word or phrase to your favorite search engine in your favorite browser. We’re not able to add to the right-click context menu or open the search in a side panel using a simple macro; you could do this using an addin, which is beyond the scope of the Lazy Programmer series.
To put this together, we take code from two macros here at slipstick.com. We start with the macro at Create Task or Appointment and Insert Selected Text to create a variable from the selected text, while removing the code that creates the task. Then we send the search URL to a browser. Done.
Thanks to the GetCurrentItem function, this macro will work with selections in the reading pane, in an open message, or when composing a message. You'll need to customize the ribbon or Quick Access Toolbar in the main Outlook window, an open message, and in the compose form.
Sub SearchGoogle() ' Set reference to Word object model ' in VBA Editor, Tools, References Dim objMail As Object 'Outlook.MailItem Dim objWord As Word.Application Dim objInsp As Inspector Dim objDoc As Word.Document Dim objSel As Word.Selection Dim strPhrase As String Dim oApp As Object On Error Resume Next Set objMail = GetCurrentItem() ' copy the selection to the clipboard Set objInsp = objMail.GetInspector If objInsp.EditorType = olEditorWord Then Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection strPhrase = objSel End If ' send to google search Dim strURL As String strURL = "https://www.google.com/search?q=" & strPhrase ' see http://slipstick.me/t0u58 if you want to use a different browser ' start browser code block Set oApp = CreateObject("InternetExplorer.Application") ' use CLng(2048) to open in a new tab in the current window oApp.navigate strURL ', CLng(2048) oApp.Visible = True 'wait for page to load before passing the web URL Do While oApp.Busy DoEvents Loop ' end browser code block Set objMail = Nothing Set objSel = Nothing Set objInsp = Nothing Set objWord = Nothing End Sub Function GetCurrentItem() As Object Dim objApp As Outlook.Application Set objApp = Application On Error Resume Next Select Case TypeName(objApp.ActiveWindow) Case "Explorer" Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) Case "Inspector" Set GetCurrentItem = objApp.ActiveInspector.CurrentItem End Select Set objApp = Nothing End Function
You can change the search engine url to any search engine. Common search urls are in the table below.
|Search Engine||URL to use|
If you prefer using Google or Firefox, you need to set the path to the browser and call it using Shell.
Replace the lines from Set oApp to Loop:
' start browser code block Set oApp = CreateObject("InternetExplorer.Application") --snip-- DoEvents Loop ' end browser code block
with this to use Chrome:
Dim browserPath As String browserPath = Chr(34) & "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" & Chr(34) ' use double quotes around multiword phrases Shell (browserPath & " -url " & Chr(34) & strURL & Chr(34)) DoEvents
How to use the macro on this page
First: You need to have macro security set to low during testing. The macros will not work otherwise.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
Set a reference to Word Object Library
This macro uses the Word Object Library. If you receive a "User-defined type not defined" error, you didn't to set a reference to the Word object library.
- Go to Tools, References menu.
- Locate the object library in the list and add a check mark to it. (Word object library version number will match your Outlook's version number.)
Add the macro to a button on the ribbon
- Open File, Options, Customize Ribbon
- Select Macros from the Choose commands from dropdown (1)
- Add a New Group to the ribbon (2)
- Select the Search macro (3)
- Click Add to add the macro to the new group. (4)
- Click Rename to give it a friendly name and an icon. (5)
More information as well as screenshots are at How to use the VBA Editor