Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Build Google Search String
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!


This Microsoft Word Tips & Microsoft Word Help page provides a handy VBA tool to easily create a Google search string from selected text.

For example, let's use the snippet of text shown below.

google srch 1

google srch 2

Install then execute the following macro:

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

VBA Script:
Sub SrchGoogle()
Dim pStr As String, pSrcText As String, pChar As String, URL As String
Dim i As Long
  pStr = Selection.Text
  i = 1
  For i = 1 To Len(pStr)
    pChar = Mid(pStr, i, 1)
    pSrcText = pSrcText + EncodeUTF8(pChar)
  Next i
'%22 is needed at the begining and the end of the string, so Google searches on the whole string
  URL = "http://www.google.com/search?q=%22" & pSrcText & "%22"
'Note, if the length is more than 487, transmited url will be shorted.
  ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    URL, ScreenTip:="Search this text with Google", _
    TextToDisplay:=Selection.Text
End Sub

Public Function EncodeUTF8(ByVal pCar As String) As String
Dim CarVal As Long
Dim Sextet As Long
Dim Quintet As Long
Dim Sextet1 As Long
Dim Sextet2 As Long
Dim Quartet As Long
CarVal = AscW(pCar)
'Ensure positive code point values
If CarVal < 0 Then CarVal = CarVal + 65536
Select Case CarVal
'Case ASCII, 1 byte coding, 7 significant bits
Case Is < 128
EncodeUTF8 = pCar
'Case 2 bytes coding, 8 to 11 significant bits (5 bits, then 6 bits)
Case Is > 127, Is < 2048
Sextet = 128 + CarVal Mod 64
CarVal = CarVal \ 64
Quartet = 224 + CarVal
EncodeUTF8 = "%" + Hex(Quartet) + "%" + Hex(Sextet1) + "%" + Hex(Sextet2)
End Select
End Function

The link is inserted at the selected text.

google srch 3

That's it! I hope you have found this tips page useful and informative.

Share