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

Changing the Maximum Undo Levels

Want to change the number of "undo" steps available when editing? You can't, because Word doesn't' really have a maximum. ...

Discover More

Task Pane Doesn't Appear Properly

Excel 2002 and 2003 include a Task pane that provides quick access to common tasks. The Task pane is normally visible when ...

Discover More

Inserting a Non-Breaking Space

In Word a non-breaking space will help you keep two words together on the same line. Here's two different ways that you can ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Drop-Down List of Hyperlinks

Creating a drop-down list with Excel's data validation feature can be a nice touch for a worksheet. What if you want the ...

Discover More

Pulling Apart a URL

Excel provides a good number of worksheet functions that can help you pick apart text strings in various ways. In this tip ...

Discover More

Converting to Hyperlinks in a Shared Workbook

When you enter a URL or e-mail address in a worksheet, Excel usually converts it to a clickable hyperlink. This doesn't ...

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

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. Maximum image size is 6Mpixels. 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 2 + 1?

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'.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

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.