Sei sulla pagina 1di 22

Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 1 of 22

(http://www.makeuseof.com/)

TOPICS (HTTP://WWW.MAKEUSEOF.COM/TOPICS/)

Q&A (HTTP://WWW.MAKEUSEOF.COM/ANSWERS/)

TOP LIST (HTTP://WWW.MAKEUSEOF.COM/PAGES/THE-


(HTTP://WWW.MAKEUSEOF.COM/PAGES/THE-BEST-
BEST-OF)

GUIDES (HTTP://WWW.MAKEUSEOF.COM/PAGES/)

PODCAST (HTTP://WWW.MAKEUSEOF.TV/)

Using VBA To Automate


Internet Explorer Sessions
From An Excel Spreadsheet

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 2 of 22

Ryan Dube (http://www.makeuseof.com/tag/author/ryandube/)

On 3rd October, 2013

Windows (http://www.makeuseof.com/service/windows/)

ByRyan Dube (http://www.makeuseof.com/tag/author/ryandube/) on3rd October, 2013 |


Windows (http://www.makeuseof.com/service/windows/) | 12 Comments

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 3 of 22

Its integration with Windows allows control of Internet Explorer in a number of surprising
ways using Visual Basic for Applications (VBA) script from any application that supports it,
such as Word, Outlook or Excel.

VBA automation especially directly automating a browser like IE as youll see in this article
is exactly the sort of thing that elevates VBA from a convenient programming script into a
powerful automation language. What makes it so awesome is the fact that many applications
with controls or objects are created simply for the purpose of allowing you to integrate into it
using the VBA programming language.

Through the years, weve showed you how to do some really cool stuff with VBA. For
example, you can use it to send emails directly from inside Excel
(http://www.makeuseof.com/tag/send-emails-excel-vba/), you can automaticallyexport
Outlook tasks to an Excel spreadsheet (http://www.makeuseof.com/tag/export-outlook-
tasks-excel-vba/), and you can even design your own Internet browser
(http://www.makeuseof.com/tag/basic-internet-browser-vba/)! It isnt just Microsoft
products either. There are 3rd-party applications from all sorts of vendors that have
integrated VBA and compatible objects into their software from Adobe Acrobat SDK to
theObjectARXSDK for AutoCAD there are ways to plug into more applications than you
probably realize.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 4 of 22

The Idea

In this case, youre going to connect Excel with IE. Why IE? Because Internet Explorer is so
well integrated with the operating system that you really dont have to do much to start using
IE automation in VBA in other Microsoft products like Word or Excel. Thats the beauty of in.
In this article youll see how this automation works, and in a future article youll see how to do
nearly the same sort of thing with other browsers.

What Im going to show you here is a seemingly simple application, but it has plenty of
applications where you could use this code to do a variety of cool things with your browser.
The bottom line is that youre going to create an Excel spreadsheet for the purpose of quickly
saving all of your open browser windows with one click of a button. You can save this
spreadsheet and walk away or turn off your computer.

Come back an hour or three days later, open the spreadsheet, click another button and those
saved URLs will reopen in the same number of tabs as you had before. The obvious cool use of
this would be to store a whole library of common online workspace setups in Excel. Then you
can restore that workspace with one click of a button without having to find all of those URLs
again.

Automating Internet Explorer with VBA

The first thing to do is open Excel (Im using 2013 other versions are similar when it comes
to VBA programming) and go to the Developer menu item. Inside there, youll see an insert
button, which drops down all of your controls. Select the ActiveX pushbutton control and
place it in your spreadsheet.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 5 of 22

Presumably, youve already created a header for URLs if you want, but you dont have to. This
is really a URL storage library, so headers dont really matter. Once you add the button,
double click on it to open up the VBA editor. To the lower left, youll see the properties for
your new pushbutton.

Rename it to something like cmdSaveURLs and set the Caption to Save URLs indicating
that this is the button to save all open URLs from your IE browser.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 6 of 22

Next, go to the Tools menu at the top of the VBA editor, click on References in the menu, and
scroll down the long list to find the Microsoft Internet Controls reference. Click the
checkbox to the left of it, and then click OK.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 7 of 22

Now youre ready to roll. In the editor text area, you should see a line that reads Private Sub
cmdSaveURLs_Click(). If you dont see it, click the left dropdown box above the text area and
find cmdSaveURLs in the list. Select it, and itll create the Click() function for you.

This is the code you want to insert into that function:

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 8 of 22

im IE As Object
Dim shellWins As New ShellWindows
Dim IE_TabURL As String
Dim intRowPosition As Integer

intRowPosition = 2

For Each IE In shellWins


IE_TabURL = IE.LocationURL
If IE_TabURL <> vbNullString Then
Sheet1.Range("A" & intRowPosition) = IE_TabURL
intRowPosition = intRowPosition + 1
End If

Next

Set shellWins = Nothing


Set IE = Nothing

The Microsoft Scripting Runtime reference makes it so that you can access the ShellWindows
object, which allows you to iterate through Windows and locate the instances of IE that you
have open. This script will locate every URL you have open and write it to the Excel
spreadsheet.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 9 of 22

So, in theory if youre working on something like blogging, and you have a few items open, like
research windows, your blog editor, or a calendar window - all of those tabs will be active. If
you have to shut down or leave in a hurry, it can be a real pain to save where you are by
copying all those URLs.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 10 of 22

With your new Excel script, just click the Load URLs button, and itll load it right into the
spreadsheet.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 11 of 22

One caveat. If you arent using a header row, then youll want to change the line
intRowPosition=2 to intRowPosition=1 and this will start at the first row rather than
skipping the header row.

Opening Your Saved Browser Workspace

The next stage of this project is to go in the other direction. Click the Load URLs and have
Excel launch IE and reload all of those URLs you have saved in the spreadsheet. Heres what
the cmdLoadURLs_Click() function should look like.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 12 of 22

Dim IE As Object
Dim shellWins As New ShellWindows
Dim IE_TabURL As String
Dim intRowPosition As Integer

intRowPosition = 2

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

IE.Navigate Sheet1.Range("A" & intRowPosition)

While IE.Busy
DoEvents
Wend

intRowPosition = intRowPosition + 1

While Sheet1.Range("A" & intRowPosition) <> vbNullString


IE.Navigate Sheet1.Range("A" & intRowPosition), CLng(2048)

While IE.Busy
DoEvents
Wend

intRowPosition = intRowPosition + 1
Wend

Set IE = Nothing

There are a few steps here, but as you can see the code isnt all that long or complicated. You
create a new instance of IE, make it visible (this will open IE without loading an URL). Next itll
load the first URL in the list.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 13 of 22

The While IE.Busy part of the script waits until the page is fully loaded, and then move on to
the rest of the URLs in your spreadsheet, opening a new tab (thats what the CLng(2048)
does, until it hits a blank cell in your spreadsheet, then itll stop opening new tabs. Heres my
IE browser with all four original tabs recovered using the Excel IE automation script.

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 14 of 22

Summary

My real goal of doing this was to have individual spreadsheets set up collections of tabs for
tasks like researching and writing on my own blog, writing on MakeUseOf, doing SEO project
work on the site, or a whole list of other roles or projects that require a saved collection of
tabs that are always used.

Using a spreadsheet to store those setups and automating opening them in a browser can
save a lot of timeand its actually pretty cool too.

Do you use any kind of IE automation in your VBA applications? See any other cool uses for
this kind of IE control from Excel? Share your thoughts and feedback in the comments section
below!

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 15 of 22

5 34 6 RATE THIS ARTICLE

Like Tweet
Check out more about: computer automation
(http://www.makeuseof.com/tags/automate/), microsoft excel
(http://www.makeuseof.com/tags/microsoft-excel/), spreadsheet
(http://www.makeuseof.com/tags/spreadsheet/)

12 Comments - Write a Comment

0
VOTES

Suvadeep P Reply
October 3, 2013

Hello Ryan,
Nice article and helpful as well. Can it be done in Google Chrome and
Firefox. Will the process remain same? Last thing is where is the LOAD
URLs BUTTON? Cannot find that.

With your new Excel script, just click the Load URLs button, and itll load it
right into the spreadsheet.

Thanks.

Ryan Dube
October 4, 2013

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 16 of 22

Yes, theres another component Ill be covering in a future article where you can do
automation like this in Chrome and Firefox. You cant use the code in this article to
automate those browsers unfortunately. The Load URLs button actually refers to
whatever you named the button when you created it. If you look at the picture I
named my button Load URLs.

0
VOTES

Lenny R Reply
October 3, 2013

Thank you for the article. When I setup up the spreadsheet, I had outlook
open and it added the following Url.
outlook:search%20folders/Unread%20Mail

Do you have an idea how I can get it to ignore Outlook when running the
commands?

Sandi
October 4, 2013

Yes, I was wondering that myself. You have to duplicate the steps you did to create the
SaveURLs button to add a LoadURLs button. The entire VBA goes like this:

Private Sub cmdSaveURLs_Click()

Dim IE As Object
Dim shellWins As New ShellWindows
Dim IE_TabURL As String
Dim intRowPosition As Integer

intRowPosition = 2

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 17 of 22

For Each IE In shellWins


IE_TabURL = IE.LocationURL
If IE_TabURL vbNullString Then
Sheet1.Range(A & intRowPosition) = IE_TabURL
intRowPosition = intRowPosition + 1
End If

Next

Set shellWins = Nothing


Set IE = Nothing

End Sub

Private Sub cmdLoadURLs_Click()

Dim IE As Object
Dim shellWins As New ShellWindows
Dim IE_TabURL As String
Dim intRowPosition As Integer

intRowPosition = 2

Set IE = CreateObject(InternetExplorer.Application)
IE.Visible = True

IE.Navigate Sheet1.Range(A & intRowPosition)

While IE.Busy
DoEvents
Wend

intRowPosition = intRowPosition + 1

While Sheet1.Range(A & intRowPosition) vbNullString


IE.Navigate Sheet1.Range(A & intRowPosition), CLng(2048)

While IE.Busy
DoEvents
Wend

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 18 of 22

intRowPosition = intRowPosition + 1
Wend

Set IE = Nothing

End Sub

hope this helps!

0
VOTES

Baylin Reply
October 3, 2013

Thats really cool!

0
VOTES

Sandi Reply
October 4, 2013

this is awesome! any chance of replicating this in MS WORD?

Ryan Dube
October 4, 2013

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 19 of 22

Hi Sandi yes, definitely. Its nearly the same code with just a few minor differences.
Manipulating the IE object is exactly the same though.

Sandi Gauthier
October 4, 2013

Thanks Ryanas a newbie, I simply copied/pasted the Excel code into wordof
courseit didnt work.would you mind providing some guidance on how to modify for
Word?

0
VOTES

Justin Hamilton Reply


October 5, 2013

Thank you for sharing this. However due to the typo in the code and the
fact the set of code I am supposed to paste does not match the picture of
the code in your example. What am I supposed to do after I paste in the
code? The instructions were very detailed up to that point.

0
VOTES

Justin Hamilton Reply


October 5, 2013

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 20 of 22

Just one more small issue: The Load URLs Button you reference above
and in a latter explanation to a reader is actually Save URLs Button in
your example. This is confusing to some of us newbies.

0
VOTES

Steakfask Reply
October 16, 2013

I will not google anal thank you very much!! :)

0
VOTES

Mennouny Reply
November 11, 2013

hello,
it looks great idea like it :) thanks for sharing .

i have Just a question : is it possible to insert information to texte box


(inputs) in internet page like searching HELLO and submet button ?

thanks a lot

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 21 of 22

Your Comment

We Teach Tech. Join The Force!

Like 248k Follow 67.5K followers Follow 113k

Popular Now

CryptoLocker Is The Nastiest Malware Ever & Heres What You Can Do
(http://www.makeuseof.com/tag/cryptolocker-is-the-nastiest-malware-ever-heres-what-
you-can-do/)

External Drive Not Recognized? This Is How To Fix It In Windows


(http://www.makeuseof.com/tag/external-drive-not-recognized-this-is-how-to-fix-it-in-
windows/)

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013
Using VBA To Automate Internet Explorer Sessions From An Excel Spreadsheet Page 22 of 22

Find Out Whos Eating Your Bandwidth With These Tips


(http://www.makeuseof.com/tag/find-out-whos-eating-your-bandwidth-with-these-tips/)

Do You Use A Third Party Firewall On Your Computer? [MakeUseOf Poll]


(http://www.makeuseof.com/tag/do-you-use-a-third-party-firewall-on-your-computer-
makeuseof-poll/)

How To Keep Your Files Synced With SkyDrive In Windows 8.1


(http://www.makeuseof.com/tag/how-to-keep-your-files-synced-with-skydrive-in-
windows-8-1/)

Your Feedback Counts

Tell us what you want to read! (https://docs.google.com/forms/d/1m1fIfvsWNtiqI2rU3gH0YR2jG7V


12 people commented. Add

Loading more
Copyright 2013, MakeUseOf. All Rights Reserved .

About (http://www.makeuseof.com/about/)
Subscribe (http://www.makeuseof.com/subscribe/)
Contact (http://www.makeuseof.com/contact-team/)
Advertise (http://www.makeuseof.com/advertise/)
Privacy (http://www.makeuseof.com/disclaimer/)

http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-fro... 19/11/2013

Potrebbero piacerti anche