Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an html page in default browser with VBA?

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.

like image 386
dmr Avatar asked Jul 02 '10 13:07

dmr


People also ask

Can a macro open a website?

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.

Can you use VBA to pull data from website?

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").


2 Answers

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.

like image 120
Dirk Vollmar Avatar answered Oct 06 '22 22:10

Dirk Vollmar


You can even say:

FollowHyperlink "www.google.com" 

If you get Automation Error then use http://:

ThisWorkbook.FollowHyperlink("http://www.google.com") 
like image 25
Rahul Narkhede Avatar answered Oct 06 '22 23:10

Rahul Narkhede