tags:

views:

30

answers:

2

How do I open an html page in the default browser with vba? I know it's something like:

Shell "http://myHtmlPage.com"

But I think I have to reference the program which will open the page...Help!

Thank you!

+1  A: 

You need to call ShellExecute.

SLaks
+1  A: 

You can use the Windows API function ShellExecute to do so:

Option Explicit

Private Declare Function ShellExecute _
  Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hWnd As Long, _
  ByVal Operation As String, _
  ByVal Filename As String, _
  Optional ByVal Parameters As String, _
  Optional ByVal Directory As String, _
  Optional ByVal WindowStyle As Long = vbMinimizedFocus _
  ) As Long

Public Sub OpenUrl()

    Dim lSuccess As Long
    lSuccess = ShellExecute(0, "Open", "www.google.com")

End Sub

Just a short remark concerning security: If the URL comes from user input make sure to strictly validate that input as ShellExecute would execute any command with the user's permissions, also a format c: would be executed if the user is an administrator.

0xA3
Just a note for anyone who might use this in the future: You have to put the ShellExecute function at the top of the page, in the declarations section.
dmr