Excel Developer Tip


Return to The Spreadsheet Page

Excel page

Tip archives

Opening a Web Document from Excel

If you use Excel 97 or later, you may know that this version supports hyperlinks. For example, you can insert into a worksheet a hyperlink that points to your WWW home page. Click the hyperlink and the Web document opens - in Excel. The result is usually not very satisfactory.

This tip demonstrates a simple technique to display Web document in the user's default Browser.

The Open WebPage subroutine

The subroutine below opens the user's default Web browser and loads the document assigned to the URL variable.

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
Sub OpenWebPage()
    URL = "http://www.j-walk.com/ss/index.htm"
    ShellExecute 0&, vbNullString, URL, vbNullString, _
       vbNullString, vbNormalFocus
End Sub

In this example, the document is a WWW site. However, it could also be an HTML document on a local hard drive. For example, the statement below would cause a local document to be opened in the browser:

    URL = "c:\webdocs\mypage.htm"

The statement below creates an email:

    URL = "mailto:billg@microsoft.com"

How it works

The procedure uses the ShellExecute API function to launch the application associated with the text in the URL variable. For a standard URL, it launches the default browser. If the URL is a "mail to" URL, it launches the default email client.