Avatar of docsimple
docsimple

asked on 

MS Word macro for opening URL in an open browser

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.

VB ScriptMicrosoft Word

Avatar of undefined
Last Comment
Francisco Igor
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

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
Avatar of docsimple
docsimple

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of docsimple
docsimple

ASKER

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)?
This option is application specific (not all browsers manage tabs)
In firefox there are options to manage that  (Tool/options/tabs)
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo