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

Macros in Template Files

People often place macros in template files to perform any number of tasks. This tip describes a situation where the link ...

Discover More

Eliminating "Before Spacing" at the Top of a Page

When formatting paragraphs in Word, you have several options to adjust the spacing before, within, and at the end of each ...

Discover More

Macro for Month Name

Need to know how to generate a full month name based on a date? It's easy to do, as discussed in this tip.

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Retrieving Web Query Data without Interruption

If you use Excel's Web Query tools to grab data from a website, you may run into some problems if the site isn't available ...

Discover More

Creating a Dynamic Hyperlink

Want to create a hyperlink that will always display a different worksheet in your workbook? There are several ways to do it, ...

Discover More

Automatic Text in an E-mail

When creating an e-mail address hyperlink using the Insert Hyperlink dialog box, Excel allows you to enter a subject for the ...

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. 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 9 + 4?

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.