Wednesday, September 24, 2025

Weird Little VBA Macro That Might Be Useful

Select a string in Word.  Run the macro.  It opens your default browser, put quotation marks around the string and searches.  If you are writing about something more serious than your favorite cat videos, that can speed up the process.

Sub SearchSelectedTextWithQuotes()

Dim selectedText As String

Dim searchQuery As String

Dim searchURL As String




' Get the selected text

selectedText = Trim(Selection.Text)




' Check if there's any text selected

If Len(selectedText) = 0 Then

MsgBox "Please select some text to search.", vbExclamation

Exit Sub

End If




' Wrap the text in quotes and encode for URL

searchQuery = Chr(34) & selectedText & Chr(34)

searchQuery = Replace(searchQuery, " ", "+")

searchQuery = Replace(searchQuery, Chr(34), "%22")




' Construct the search URL

searchURL = "https://www.google.com/search?q=" & searchQuery




' Open the default browser with the search URL

Shell "cmd /c start " & searchURL, vbHide

End Sub

 Getting that into your Normal.dotm template so it always appears may be some work.

No comments:

Post a Comment