Word

Google search on selection from current document

Ease of Use

Intermediate

Version tested with

2000, 2003 

Submitted by:

Brandtrock

Description:

This shows how to perform a Google search from the current document. The selected text at the time the button is clicked will be the search term. Thanks to Suat (Smozgur) for helping me fix a bug in this! 

Discussion:

Employees who need to research unfamiliar terms in a training document could make use of this functionality. I use it to let my sons quickly search for unfamiliar terms in documents I have them read to supplement the education they get at school. 

Code:

instructions for use

			

Option Explicit Dim MyRange As Range Dim WithEvents WordApp As Word.Application Dim MySearch As String Dim MyText As String Private Sub Document_Open() Set WordApp = Word.Application End Sub Private Sub GoogleSearch_click() If MyRange Is Nothing Then Exit Sub MyRange.Select 'Check whether the selection is only a blinking insertion point 'If it is, extend the range to include the entire word 'the IP is in, or the one to which it is directly adjacent If Selection.Type = wdSelectionIP Then 'Comment out the following line if retaining 'a bracket only, and not highlighting an entire word 'is desired if there is no selection Selection.Words(1).Select End If MyText = Selection.Text MySearch = InputBox("What would you like to search for?", "Internet Search Tool", MyText) If MySearch = "" Then Exit Sub ActiveDocument.FollowHyperlink Address:="http://www.google.com/search?hl=en&q=" & MySearch, NewWindow:=True End Sub Private Sub WordApp_WindowSelectionChange(ByVal Sel As Selection) If Len(Selection) = 1 And Asc(Selection) = 1 Then Exit Sub Set MyRange = Selection.Range End Sub

How to use:

  1. To add a button to assign the macro to:
  2. Select View>Toolbars>Control Toolbox
  3. The CommandButton icon is a rectangle. Select that icon by clicking on it.
  4. After the command button is added to the document, resize it by dragging the corner of the button until it is the desired size.
  5. Click the button once.
  6. Select the Properties button from the Control Toolbox menu.
  7. In the box next to (Name), type GoogleSearch to rename the button.
  8. Click the X in the top right corner of the properties window to dismiss it and save the changed name.
  9. Select the View Code button from the Control Toolbox menu.
  10. Once the Visual Basic Editor has launched, there will be some text in the code window. Select all of this and delete it.
  11. Copy the code from the window above and paste it into the code window of the Visual Basic Editor.
  12. Return to Word.
  13. Save the document.
  14. Select some text and press the button.
  15. The input box will have the selected text as the search term.
  16. Click OK if to search the term as it appears.
  17. Adjust the text if desired.
 

Test the code:

  1. Launch the attached document.
  2. Clicking the Google Search button will launch an Input box.
  3. The default value in the input line will be:
  4. The selected text when the button was clicked.
  5. The word that the insertion point (blinking cursor) was in when the button was clicked.
  6. In either case, the default text may be added to, deleted, or shortened in the input box.
  7. Clicking OK will launch a new internet browser window with the Google search results displayed and the search term in the box on that page.
 

Sample File:

GoogleSearch.zip 7.73KB 

Approved by mdmackillop


This entry has been viewed 106 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express