Google Hacks
Google Hacks
Google Hacks
2nd
Cove
n
HACKS
Editio ail
rs Gm
HACK
#63 Google from Word
You probably use Google a few dozen times a day. If you work a lot within
Microsoft Word, using Google usually means switching over to your web
browser, checking the results, and then going back to Word. This hack will
show you how to display the search results in Word’s New Document Task
Pane.
This hack uses a plain text .ini file to store data and some Visual Basic for
Applications (VBA) code that also uses VBScript regular expressions.
This hack will work only with Word 2003 for Windows.
Using Google from Word requires a bit of setup, but once you’ve installed
the appropriate tools, you can use Google from within any Word macro.
HACK
Google from Word #63
HACK
#63 Google from Word
The Code
With the GoogleTools.dot template you created open, select Tools ➝ Macro
➝ Macros and insert the following code, which consists of a procedure
named GoogleToTaskPane and a supporting function named StripHTML.
Make sure you replace the value insert key here with your
Google API developer’s key.
Sub GoogleToTaskPane( )
Dim vSearchResults As Variant
Dim v As Variant
Dim sResults As String
Dim sEntryName As String
Dim sEntryURL As String
Dim sLogFile As String
Dim sSearchDisplayTitle As String
Dim sSearchURL As String
Dim i As Integer
HACK
Google from Word #63
HACK
#63 Google from Word
str_oe:=sOutputEncoding).resultElements
System.PrivateProfileString( _
FileName:=sLogFile, _
Section:="GoogleTaskPane", _
Key:="URLName" & CStr(i)) = sSearchURL
System.PrivateProfileString( _
FileName:=sLogFile, _
Section:="GoogleTaskPane", _
Key:="EntryName" & CStr(i)) = sSearchDisplayTitle
i = i + 1
Next v
End Sub
HACK
Google from Word #63
This hack uses two parts of the Google search results: the URLs and titles.
Google formats the search result title as HTML, but you can only put plain
text in the Task Pane. The StripHTML function uses a few simple VBScript Reg-
ular Expressions to strip out common HTML tags (such as <b>) and replace
character entities (such as @) with their ASCII character equivalents.
It can be tricky to remove files from the Task Pane using VBA unless you
know their exact name. This macro, however, stores the search results in a
plain-text .ini file. The next time you do a search, you can easily remove the
previous results. The macro uses a file named C:\google_taskpane.ini, which
is defined in the GoogleToTaskPane procedure.
Figure 5-22. Entering a Google search that will display in the Task Pane
Enter your search terms and click the OK button. The New Document Task
Pane appears and displays the search results, as shown in Figure 5-23. Hover
your mouse over any of the entries to display the URL. Click a URL to open
the site in your web browser.
HACK
#63 Google from Word
Every time you run a search, the macro removes the previous results from
the Task Pane. If you want to remove the previous results without display-
ing new ones, click the Cancel button in the dialog box shown in
Figure 5-22.
HACK
Google from Word #63
HACK
#63 Google from Word
Next i
System.PrivateProfileString( _
FileName:=sLogFile, _
HACK
Google from Word #63
Section:="GoogleTaskPane", _
Key:="URLName" & CStr(i)) = sSearchURL
System.PrivateProfileString( _
FileName:=sLogFile, _
Section:="GoogleTaskPane", _
Key:="EntryName" & CStr(i)) = sSearchDisplayTitle
i = i + 1
Next v
End Sub
To help ensure a good Google search, the following two lines collapse two
ends of the selection if they contain spaces or a paragraph mark:
Selection.MoveStartWhile cset:=Chr$(32) & Chr$(19), _
Count:=Selection.Characters.Count
Selection.MoveEndWhile cset:=Chr$(32) & Chr$(19), _
Count:=-Selection.Characters.Count
—Andrew Savikas