MS Word macro for opening URL in an open browser

docsimple
docsimple used Ask the Experts™
on
I desperately want a MS Word macro that will look up a highlighted phrase in Google.

I'm currently using the following VBA code:

 Set oIE = CreateObject("InternetExplorer.Application")
 oIE.Visible = True
 oIE.Navigate (myURL)

Open in new window


However, this macro opens Internet Explorer in a new window each time I run it. This takes a few seconds and leaves lots of IE windows open after a few searches.

Is there any way of modifying it so that it doesn't open IE in a new window when IE is already open? Also, how would I change the code to use Chrome rather than IE?

Any suggestions would be greatly appreciated.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
F IgorDeveloper

Commented:
Use the folowing code in a vb module:
(Use the shellexecute API function. For an url, it opens the default system's browser)

Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long


Private Sub openurl(url as String)
ShellExecute 0, "OPEN", url, "", "", 0
End Sub




'A direct call example
ShellExecute 0, "OPEN", "http://www.google.com", "", "", 0

Author

Commented:
Many thanks for that code, fraigor.

I created and new macro in Word (File/Macro/View Macros/Create) and pasted the code into the new macro. But when I ran it, I got the following error message:

"Compile error:

Only comments may appear after End Sub, End Function, or End Property.
"

(and it highlights the Declare Function statement.)

Am I doing something wrong?
Developer
Commented:
The final code :::
==========
'A direct call example
ShellExecute 0, "OPEN", "http://www.google.com", "", "", 0
====

paste the folowing code instead::






Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long


Sub OpenUrl()
'myUrl needs to be set from an external reference
myURL = "http://www.google.com"
ShellExecute 0, "OPEN", myURL, "", "", 0
End Sub

Author

Commented:
Thanks for clarifying that. It's no running fine. Thank you so much for your assistance.

I just have one final question before I close the question: is it possible to modify the statement so that it opens in the currently open tab in the browser (currently, it opens a new tab for each search)?
F IgorDeveloper

Commented:
This option is application specific (not all browsers manage tabs)
In firefox there are options to manage that  (Tool/options/tabs)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial