I found an article with a Macro that can selected text in the Microsoft Word Document and search it on Google: https://www.datanumen.com/blogs/quickly-search-selected-text-google-yahoo-bing-word-document/
However, the first row's code "Dim objIE As Object" makes it cannot be ran on my computer since my company has uninstalled the Internet Explorer (IE) many years ago. And the current Microsoft Edge API does not allow such method.
Sub OpenBrowser(strAddress As String, Menubar As Boolean, nHeight As Long, nWidth As Long, varResizable As Boolean)
Dim objIE As Object
' Create and set the object settings.
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = False
.width = nWidth
.height = nHeight
.Menubar = Menubar
.Visible = True
.resizable = varResizable
.Navigate strAddress
End With
End Sub
Sub SearchOnGoogle()
Dim strText As String
Dim strButtonValue As String
strButtonValue = MsgBox("Do you want to search the selected text on Google?", vbYesNo, "Search on Google")
If strButtonValue = vbNo Then
Exit Sub
Else
' Make sure there is text selected.
If Selection.Type <> wdSelectionIP Then
strText = Selection.text
strText = Trim(strText)
Else
MsgBox ("Please select text first!")
Exit Sub
End If
' Search selected text on Google with browser window opened in set size.
OpenBrowser "https://www.google.com/search?num=20&hl=en&q=" & strText, True, 550, 650, True
End If
End Sub
Then, I have written the following Macro to select the word in MS Word and then search on Google. But it can only search one word only. If multiple words (such as "Social Capital") is selected and ran this Macro, the Chrome will pop-out two times and search "Social" and "Capital" separately.
Sub Google_Search_Single_Word()
Dim theTerm As String
Dim strURL As String
Dim arrSites(1)
Dim appPath As String
Dim strText As String
Dim strButtonValue As String
appPath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
If Selection.Type = wdSelectionIP Then
theTerm = Selection.Words(1).Text
Else
theTerm = Selection.Text
End If
arrSites(1) = "http://www.google.com/search?hl=en&q=" theTerm
For i = 0 To 1 Step 1
strURL = arrSites(i)
Shell (appPath & " -url " & strURL)
Next i
End Sub
Thus I found a version of Excel VBA Macro from the website:https://excelchamps.com/blog/vba-code-search-google-chrome/, which is also applicable to MS Word. However, this is a method that pop-out a box to search. If you don't type anything on that, it still automatically open the Google Chrome, which is not user-friendly.
Sub GoogleSearch()
Dim chromePath As String
Dim search_string As String
Dim query As String
query = InputBox("Please enter the keywords", "Google Search")
search_string = query
search_string = Replace(search_string, " ", " ")
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url http://www.google.com/search?hl=en&q=" & search_string)
End Sub
I'm thankful that I can enjoy the above contributions from different experts. Does anyone know how can I edit one of the above versions to make a Macro that can quickly search selected text on Google in the Microsoft Word Document?
CodePudding user response:
Here's a version of the Google_Search_Single_Word
Sub that can handle multiple words. It uses the helper function URLEncode, and you will need to include the Microsoft ActiveX Data Objects library in your project (Tools > References. If there are multiple versions available, go with the highest version number).
URLEncode is from this answer.
Sub Google_Search_Selected_Text()
Dim theTerm As String
Dim strURL As String
Dim arrSites(1)
Dim appPath As String
Dim strText As String
Dim strButtonValue As String
appPath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
theTerm = URLEncode(Selection.Text)
MsgBox theTerm
arrSites(1) = "http://www.google.com/search?hl=en&q=" theTerm
For i = 0 To 1 Step 1
strURL = arrSites(i)
Shell (appPath & " -url " & strURL)
Next i
End Sub
Public Function URLEncode( _
ByVal StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
Dim bytes() As Byte, b As Byte, i As Integer, space As String
If SpaceAsPlus Then space = " " Else space = " "
If Len(StringVal) > 0 Then
With New ADODB.Stream
.Mode = adModeReadWrite
.Type = adTypeText
.Charset = "UTF-8"
.Open
.WriteText StringVal
.Position = 0
.Type = adTypeBinary
.Position = 3 ' skip BOM
bytes = .Read
End With
ReDim Result(UBound(bytes)) As String
For i = UBound(bytes) To 0 Step -1
b = bytes(i)
Select Case b
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
Result(i) = Chr(b)
Case 32
Result(i) = space
Case 0 To 15
Result(i) = "%0" & Hex(b)
Case Else
Result(i) = "%" & Hex(b)
End Select
Next i
URLEncode = Join(Result, "")
End If
End Function
Go to Google, type in two words (e.g. dog food
), and look at the resulting URL. Notice that the space between dog
and food
has been replaced with a
symbol (https://www.google.com/search?q=dog food
). This is called URL escaping and is necessary for the URL to be properly interpreted. The original version of the Sub does not escape the URL, so Google only picks up the first word.
The version I posted escapes the URL to ensures that spaces are converted into
, as well as handling other characters that need to be escaped such as !
-> !
, ?
-> ?
, etc.