View Full Version : Solved: Follow Hyperlink with "&"
lynnnow
06-11-2010, 07:55 AM
Hi all,
I use the following macro to search for text on Google:
Sub FollowLink()
Application.Run "Highlight"
ActiveDocument.FollowHyperlink Address:="http://www.google.co.in/search?hl=en&q=" & Selection.Text, NewWindow:=True
End Sub
I just select the text and use the shortcut to search the selected text on Google. However, if the selected text has an "&" in it, the selection ends with the text limited up to the "&". Is there a way to let the macro know that the "&" is intended to be searched and not a keyword?
Lincoln
Paul_Hossler
06-11-2010, 09:14 AM
replace the '&' with the hex equivalent '%26'
It should work with the search, like "cats & dogs" to "cats %26 dogs"
ActiveDocument.FollowHyperlink Address:="http://www.google.co.in/search?hl=en%26&q=cats %26 dogs", NewWindow:=True
Paul
lynnnow
06-11-2010, 09:23 AM
Paul, the problem is I need to do this on the fly for eg.
With the text "working for Jonathan & Flynn Co." I select "Jonathan & Flynn Co" and hit the shortcut key, but only "Jonathan" will be searched, the "& Flynn Co" text does not get included in the selection. The position of the "&" is not fixed. It can be after one word or maybe after three or four words. I just select the text and hit the shortcut key. Is there a way to replace the "&" for searching in Google?
fumei
06-11-2010, 12:37 PM
Here is a possible alternative, but I must say that fussing with string literals can be, well, fussy. Dealing with "&" and that period at the end - Co. - is, well fussy, because "." is (to Word) a word in itself.
Sub GoogleThis()
Dim strOut As String
Dim j As Long
strOut = Trim(Selection.Words(1))
For j = 2 To Selection.Words.Count
If Selection.Words(j) = "." Then
strOut = strOut & Selection.Words(j)
Else
If Selection.Words(j) <> "& " Then
strOut = strOut & "+" & Trim(Selection.Words(j))
Else
strOut = strOut & "+%26"
End If
End If
Next
CreateObject("WScript.Shell").Run _
"http://www.google.ca/#hl=en&source=hp&q=" & strOut
End Sub
Note that using WScript.Run and the "http" essentially does the same thing as FollowHyperlinkAddress - it loads the default browser.
Jonathan & Flynn Co.
is sent as: Jonathan+%26+Flynn+Co.
Paul_Hossler
06-11-2010, 01:37 PM
I don't know if this covers all the cases that Gerry's does
ActiveDocument.FollowHyperlink Address:="http://www.google.co.in/search?hl=en%26&q=" & _
Replace(Selection.Text, "&", "%26"), NewWindow:=True
Paul
fumei
06-11-2010, 01:57 PM
It may or may not in fact be better. It depends on how Googgle interprets:
Jonathan %26 Flynn Co. (yours)
Jonathan+%26+Flynn+Co. (mine)
N.B. the Replace does NOT work using CreateObject("WScript").
lynnnow
06-12-2010, 02:14 AM
Thanks Gerry and Paul, both the options are good.
However, using Gerry's option, if I search the text "K&B Drug Store" only "K" is searched, though the address bar does include the entire selected text. I tried using %20 to create the html space instead of the "+", but still the result was "K" being searched. If I use "K & B Drug Store" the result is correct. This is Gerry's version.
Moving on to Paul's version. This works super for me. Even though there is a space or a lack thereof in the selected text, the entire selected text is searched for.
Great option Paul. Thanks. It works!
Lincoln :friends: :beerchug: :bow:
Paul_Hossler
06-12-2010, 06:11 AM
The way I understand Google search syntax ---
cats dogs = all documents that contain both words somewhere (w/o quotes)
cats+dogs = all documents that contain cats and exactly dogs (w/o quotes)
"cats & dogs" = all documents containing the phrase cats & dogs (with quotes)
" cats & dogs " = all documents containing the phrase cats & dogs, since it cleans the extra spaces, and probably some other things also
Explictly searching for "cats %26 dogs" directly in Google doesn't work, since Google intrepets the '%' as h25 followed by a literal "26"
So the %26 is just the way to construct the URLto link to (to which to link???)
BTW, entering 'cats dogs -birds' will find documents containing both cats and dogs, but not birds. Helpful to try and narrow the search sometimes
Paul
Basic and Advance search tips if you're interested
http://www.google.com/support/websearch/bin/answer.py?answer=134479
http://www.google.com/support/websearch/bin/answer.py?answer=136861
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.