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.
The Macro. This is a simple macro and it will open the website into Internet Explorer by default. To use the macro, change http://www.google.com to whatever website you want the user to visit. You can also replace this with a variable that holds the website url in order to make this a bit more versatile.
You can use VBA to extract data from web pages, either as whole tables or by parsing the underlying HTML elements. This blog shows you how to code both methods (the technique is often called "web-scraping").
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
As given in comment, to make it work in 64-bit, you need add PtrSafe
in the Private Declare Line as shown below:
Private Declare PtrSafe Function ShellExecute _
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.
You can even say:
FollowHyperlink "www.google.com"
If you get Automation Error then use http://
:
ThisWorkbook.FollowHyperlink("http://www.google.com")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With