Link to home
Start Free TrialLog in
Avatar of r_may
r_may

asked on

Open default browser from VBA Command Button with specific URL

Hi,

I successfully used the following code behind a Word VBA command button which I got from a website.  However, it opens only Internet Explorer.  I would actually like to open the user's default brower.  What do I need to change?  The URL is just an example below and does not exist.
============
Private Sub CommandButton1_Click()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
   IE.navigate "http://www.example.org/cgi-bin/FileManager/Manager.pl?
IE.Visible = True
End Sub

Thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of CDirenzi
CDirenzi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r_may
r_may

ASKER

I actually found another solution.  Thanks for your reply.
The "Accepted Solution" doesn't provide *any* solution to this question at all. For anyone still seeking the solution, see the attached code, which includes the necessary declarations for the relevant Win32 API call, and a corresponding VBA Function to implement it.

Rohan Moore
Option Explicit
 
Enum W32_Window_State
    Show_Normal = 1
    Show_Minimized = 2
    Show_Maximized = 3
    Show_Min_No_Active = 7
    Show_Default = 10
End Enum
 
Private 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
 
Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean
 
' Opens passed URL with default application, or Error Code (<32) upon error
 
    Dim lngHWnd As Long
    Dim lngReturn As Long
 
    lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
        vbNullString, WindowState)
   
    OpenURL = (lngReturn > 32)
End Function

Open in new window

@ rohan I tried the above code but Im a VBA noob. When I run the openURL macro I get an "argument not optional" error. Presumably I am not passing in a URL variable but where / how do I do that?
I have worked out how to pass in the URL using

Sub TestMacro()
  Application.ActiveDocument.Select
  Selection.Copy
  OpenURL "http://localhost:8500/index.cfm?wordContent=" & Selection, W32_Window_State.Show_Maximized
End Sub

Is there a way to only open a new browser window if one is not already open?
Sub TestMacro()
  Application.ActiveDocument.Select
  Selection.Copy
  OpenURL "http://localhost:8500/index.cfm?wordContent=" & Selection, W32_Window_State.Show_Maximized
End Sub

Open in new window