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?