Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Opening an HTML Page in a Macro.

Opening an HTML Page in a Macro

by Allen Wyatt
(last updated May 6, 2016)

4

Excel is a "Web aware" program, meaning that it knows how to handle hyperlinks. You can add a hyperlink in a document, click on that link, and Excel opens your Web browser and displays the contents of that link in the browser. (You can also create a hyperlink to other Office documents, including Excel workbooks.) You can even create hyperlinks to different objects on your worksheet, such as a command button in a form.

What if you want to start the browser and open an HTML file from within a VBA macro, however? There are a couple of ways that you can do this. The first is to simply open a new Internet Explorer object within your code. A macro to do this would appear as follows:

Sub DoBrowse1()
    Dim ie As Object
    Set ie = CreateObject("Internetexplorer.Application")
    ie.Visible = True
    ie.Navigate "c:\temp\MyHTMLfile.htm"
End Sub

This macro will open the file c:\temp\MyHTMLfile.htm in a new Internet Explorer window. If you want to instead open a Web page from over the Internet, you can do so simply by changing where you want to navigate. (Replace the file path with a URL.)

Another way to accomplish the same task is to rely on Excel to figure out what your default browser is and open the HTML resource. The following macro does the trick:

Sub DoBrowse2()
    ActiveWorkbook.FollowHyperlink _
      Address:="c:\temp\MyHTMLfile.htm", _
      NewWindow:=True
End Sub

Again, the browser opens a new window and displays the specified file. You can change the Address parameter to any URL that you desire.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (154) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Opening an HTML Page in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Deleting Everything Up to a Character Sequence

Sometimes you have too much information in a cell and you need to "pare down" what is there to get to the info you really ...

Discover More

Line Numbering and Tables

Some types of documents (such as legal documents) may require that individual lines of text be numbered. If you use tables to ...

Discover More

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro in ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

MORE EXCELTIPS (RIBBON)

Links to Hyperlinks

Hyperlinks in a worksheet can be helpful or essential, depending on the nature of your data. If you create a link to a ...

Discover More

Extracting URLs from Hyperlinks

When you add a hyperlink to a worksheet, it consists of a minimum of two parts: display text and URL address. If you have a ...

Discover More

Adding Excel Information to a Web Page

Besides saving a worksheet as a complete Web page, you can also save smaller portions of your data to an existing Web page. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 8?

2016-09-07 10:43:20

Zoltan Breller

Works like a charm
Thank you.


2016-05-07 03:29:56

Wellsley Over

Hi Allen, I want to thank you if I haven't already for for all the great tips I get. Being a beginner to vba the tips are money in the bank.


2014-10-14 09:22:06

Bigger Don

@Michael...

It looks like it is possible but I wouldn't necessarily trust the re-usability, stability or portability.

If you really want to do this here are the steps.
1. Do a Google search
2. Copy the URL.
3. Replace "c:tempMyHTMLfile.htm" with the URL you copied.

Of course you would probably want to make the last element of the URL, the search term, into a variable to plug into the search. That way you could re-use the code for different situations, perhaps even called by a UDF in the UI.

This brings up an issue. Let's say your user types in a one word term then you are OK. If they type a multi-word query term, or you've constructed one in your code, then you'll need to do some translating: replace spaces with plus ("+") signs, replace quotes with "%22", and I suspect there are other situations.

Something else I mentioned was portability. DoBrowse1 uses ActiveX. Creating an ActiveX component for Firefox and Chrome did not work straightaway for me. I do not know if it is possible.

Here are the three URLs for Chrome, Firefox, and IE.
* Chrome: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=autozone
* Firefox: https://www.google.com/#safe=off&q=autozone
* IE: https://www.google.com/?gws_rd=ssl#q=autozone


As I see it, the URL includes browser settings, and the way the settings show up in the URL differs. You would need to test for the effect of different browser settings. You would need to test any browsers you do support, of course, and different versions of of each because I would not trust any of them to be fully consistent from one release to the next.

...or you can force everyone to use on release of I.E. :)


2014-10-13 15:09:57

Michael

First, THANK YOU for the instructions, they work wonderfully. Now, how might I get it to search? Have excel open to google and then search 'autozone'.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share