|
|
|
|
|
|
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
If Selection.Type = wdSelectionIP Then
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:
|
- To add a button to assign the macro to:
- Select View>Toolbars>Control Toolbox
- The CommandButton icon is a rectangle. Select that icon by clicking on it.
- After the command button is added to the document, resize it by dragging the corner of the button until it is the desired size.
- Click the button once.
- Select the Properties button from the Control Toolbox menu.
- In the box next to (Name), type GoogleSearch to rename the button.
- Click the X in the top right corner of the properties window to dismiss it and save the changed name.
- Select the View Code button from the Control Toolbox menu.
- Once the Visual Basic Editor has launched, there will be some text in the code window. Select all of this and delete it.
- Copy the code from the window above and paste it into the code window of the Visual Basic Editor.
- Return to Word.
- Save the document.
- Select some text and press the button.
- The input box will have the selected text as the search term.
- Click OK if to search the term as it appears.
- Adjust the text if desired.
|
Test the code:
|
- Launch the attached document.
- Clicking the Google Search button will launch an Input box.
- The default value in the input line will be:
- The selected text when the button was clicked.
- The word that the insertion point (blinking cursor) was in when the button was clicked.
- In either case, the default text may be added to, deleted, or shortened in the input box.
- 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.
|
|