PDA

View Full Version : Open web browser from a word document



sk_uc04
09-24-2007, 12:01 PM
Hello Everybody,

Can somebody help me how can I open a web browser from a word document?
I am trying to run a macro which will open a web browser when I press Alt+F2 on the word document.
Please help!!


Thanks in advance,
SK

TonyJollans
09-24-2007, 01:19 PM
You should be able to do something like:

Set IE = CreateObject("InternetExplorer.Application")

But most likely you don't just want the browser, but you also want to browse to somewhere, in which case you can use:

ActiveDocument.FollowHyperlink "about:blank"

with your own URL of course.

sk_uc04
09-24-2007, 02:20 PM
Thanks Tony, I used the following and it worked fine.

ActiveDocument.FollowHyperlink _
Address:="http://www.Microsoft.com", _
NewWindow:=True

Thanks for your guidance.

sk_uc04
09-24-2007, 02:27 PM
Hello everybody,

I am trying to open a web browser for example www.google.com (http://www.google.com) and in the search field want to have the text/value of the textfield on my word document.

For eg:
On the word document, I have word "Yoga" entered in on of my text fields and when I press/enter Alt+F1, a web browser http://www.google.com opens. Now I want word "Yoga" entered in the google search text field.

Is there a way to carry the text field value in a session to take to web browser? I am kind of lost and not sure what to do about that.
Thanks everybody for the guidance and help.

Regards,
SK

Brandtrock
09-25-2007, 01:02 AM
This KB entry (http://vbaexpress.com/kb/getarticle.php?kb_id=309) may be of some use to you.

Regards,

fumei
09-25-2007, 08:01 AM
That KB entry is very handy. I used it to put a quickie macro in Word. It uses a keyboard shortcut - Alt-g - so I can Alt-g, get an Inputbox, type in my search string, press Enter...and IE opens with the Google result. No need to do anything else. You get the result of the search string.Sub Google()
' keyboard shortcutr = Alt-g
Dim strIn As String
strIn = InputBox("Search for?", "Open a Google Search")
If Documents.Count = 0 Then Documents.Add
ActiveDocument.FollowHyperlink _
Address:="http://www.google.com/search?hl=en&q=" & _
strIn, NewWindow:=True
End SubThe If Documents.Count is required because it will not work if there is no document open. It uses ActiveDocument...so, ummm, if there IS no ActiveDocument, it causes a run-time error.

You can easily adjust it to take ANY string. Say as an OnExit macro for a text formfield.Dim strIn As String
strIn = ActiveDocument.Formfields("Text1").Result
ActiveDocument.FollowHyperlink _
Address:="http://www.google.com/search?hl=en&q=" & _
strIn, NewWindow:=True

Brandtrock
09-25-2007, 10:49 AM
I like that enhancement Gerry. :bow::bow:

Of course, to make it a feature a la M$, we'd have to code a random number that would cause it to go to the blue screen of death every so often; then promise a fix; then release a fix that caused a different more disturbing problem or perhaps an easy security breach. :rofl:

That would really be big league!!

Regards,

Nelviticus
09-26-2007, 05:06 AM
This is cool. I like this a lot.

How about this for an over-engineered example:
Public Sub SearchGoogle()

Const sWebSite As String = "http://www.google.com/search?q="
Dim sSearch As String

If Documents.Count > 0 Then

With Selection.Range
If .End = .Start Then .Expand (wdWord)
sSearch = .Text
End With

' Get rid of special characters
sSearch = CleanString(sSearch)
sSearch = Replace(sSearch, vbCr, " ")
sSearch = Replace(sSearch, vbTab, " ")
sSearch = Trim(sSearch)

If Len(sSearch) = 0 Then
sSearch = InputBox("Enter a term to search for", "Search Google")
sSearch = Trim(sSearch)
End If

' Get rid of doubled-up spaces
Do While Len(sSearch) > Len(Replace(sSearch, " ", " "))
sSearch = Replace(sSearch, " ", " ")
Loop

If Len(sSearch) > 0 Then
sSearch = Replace(sSearch, " ", "+")
ActiveDocument.FollowHyperlink Address:=sWebSite & sSearch
End If

End If

End Sub
If your cursor's in a word, it searches google for that word. If you've highlighted several words, it searches for all the words. If you're not in a word and haven't selected anything, it asks you what you want to search for. Fun!

Regards

fumei
09-26-2007, 01:05 PM
Ummmm, yeeessssss, it is a wee bit over-engineered, but....HEY!...it is Microsoft. So go nuts.

Frankly, I would not use it much (as is), as I doubt I would want to use an existing word in the document...but you never know. I would, most likely, prefer to make an explicit input. But again, you never know.

However, the parsing of the input string for multiple words and the replacement for "+" is a brilliant improvement.

I like it and have made the change in my own wee macro. Thanks.