
It's been a while since I posted anything related to Internet Explorer, but here is a series of VBA functions that let you automate Internet Explorer's native methods.
I've received many questions about the code I posted at Automate Internet Explorer, so it's about time it was updated and refreshed with better coding techniques. This will be the first in a short series of articles about automating Internet Explorer.
We're all familiar (I hope) with the late-bound method for instantiating a copy of IE. Here is a function that returns the InternetExplorer object:
Function GetIE() As Object On Error Resume Next Set GetIE = CreateObject("InternetExplorer.Application") End Function
Call the function like this:
Dim ie As Object Set ie = GetIE
You will notice that it is relatively slow compared to the XMLHTTP Object that we've seen previously. The only time I recommend using it is when you need to actually interact with a website in order to retrieve information. Those are the bad websites that need to be shut down! Seriously, people, set up an API.
The MSDN entry for the InternetExplorer Object gives you all the methods, events and properties you'll need to make IE dance for you.
The following is a series of functions that encapsulate most of the methods and properties. For most/all of these functions, you will need to pass in an InternetExplorer Object. I didn't do a type check but if you want to add one, check that TypeName(obj) = "IWebBrowser2". That's what the IE Object was referred to on my machine (YMMV).
If you want an example of class-driven events, see VBA web services.
AddressBar Property
Check if the Address Bar is visible using this function. If you pass True or False as the second parameter, you can change the property value.
Function ShowAddressBar(inet As Object, Optional addrBar As Variant) As Boolean
If Not IsMissing(ShowAddressBar) Then
If CBool(ShowAddressBar) Then
inet.AddressBar = ShowAddressBar
End If
End If
ShowAddressBar = inet.AddressBar
End FunctionBusy Property
Check if your IE object is busy loading a page by passing it to this function.
Function IsIEBusy(inet As Object) As Boolean IsIEBusy = inet.Busy End Function
FullScreen Property
This function lets you get or set the FullScreen Property of IE. Personally I hate IE in full-screen.
Function IEFullScreen(inet As Object, _
Optional displayFullScreen As Variant) As Boolean
If Not IsMissing(displayFullScreen) Then
If CBool(displayFullScreen) Then
inet.FullScreen = displayFullScreen
End If
End If
IEFullScreen = inet.FullScreen
End FunctionLocationName Property
This property returns the title bar caption from the IE window.
Function GetIELocation(inet As Object) As String GetIELocation = inet.locationName End Function
LocationURL Property
If you want the current URL, this is the function to use.
Function GetIEURL(inet As Object) As String GetIEURL = inet.LocationURL End Function
MenuBar Property
Set or get the MenuBar Property that determines whether the IE menu bar is visible.
Function DisplayIEMenuBar(inet As Object, _
Optional displayMenu As Variant) As Boolean
If Not IsMissing(displayMenu) Then
If CBool(displayMenu) Then
inet.MenuBar = displayMenu
End If
End If
DisplayIEMenuBar = inet.MenuBar
End FunctionReadyState Property
The ReadyState Property works similar to the Busy Property. It gives you the state of the IE object. I've also provided the Enum so you can see all the possible return values.
Function GetIEReadyState(inet As Object) As Integer GetIEReadyState = inet.READYSTATE End Function 'Enum READYSTATE ' READYSTATE_UNINITIALIZED = 0 ' READYSTATE_LOADING = 1 ' READYSTATE_LOADED = 2 ' READYSTATE_INTERACTIVE = 3 ' READYSTATE_COMPLETE = 4 'End Enum
Most of the time you'll be checking for the number 4 (complete) using a Do While GetIEReadyState(ie) <> 4 type of statement.
Resizable Property
If you want your IE window to stay a certain size, you'll want to set the Resizable Property to False. Here's one way to do it.
Function IEResizable(inet As Object, Optional resize As Variant) As Boolean
If Not IsMissing(resize) Then
If CBool(resize) Then
inet.Resizable = resize
End If
End If
IEResizable = inet.Resizable
End FunctionIf you pass in True or False as the second parameter, you can adjust the value. Either way, the function returns the current value.
Silent Property
This property would be useful if you are expecting to navigate to a site with popups or dialog boxes you don't want to see. Especially those script debugging boxes that seem to appear with IE.
Function IESilent(inet As Object, Optional keepSilent As Variant) As Boolean
If Not IsMissing(keepSilent) Then
If CBool(keepSilent) Then
inet.Silent = keepSilent
End If
End If
IESilent = inet.Silent
End FunctionStatusBar Property
Hide or show the status bar for the current IE window with this function.
Function IEStatusBar(inet As Object, Optional statusBar As Variant) As Boolean
If Not IsMissing(statusBar) Then
If CBool(statusBar) Then
inet.StatusBar = statusBar
End If
End If
IEStatusBar = inet.statusBar
End FunctionStatusText Property
Set or get the status bar text with this function. You'll want to make sure the status bar is visible first.
Function IEStatusBarText(inet As Object, _
Optional statusBarText As String) As String
If Len(statusBarText) > 0 Then
inet.StatusText = statusBarText
End If
IEStatusBarText = inet.statusText
End FunctionTheaterMode Property
This function will put IE into Theater mode. That's where all the toolbars disappear and IE is maximized on the desktop.
Function IETheaterMode(inet As Object, _
Optional isTheaterMode As Variant) As Boolean
If Not IsMissing(isTheaterMode) Then
If CBool(isTheaterMode) Then
inet.TheaterMode = isTheaterMode
End If
End If
IETheaterMode = inet.TheaterMode
End FunctionVisible Property
Make the IE window visible with this function. When you instantiate IE, the window remains invisible. According to MSDN,
When the Windows Internet Explorer application is first created, it is hidden. It becomes visible after the Navigate method or the GoSearch method is used.
In my experience, it remains invisible until you set the Visible Property to True.
Function IEVisible(inet As Object, Optional isVisible As Variant) As Boolean
If Not IsMissing(isVisible) Then
If CBool(isVisible) Then
inet.Visible = isVisible
End If
End If
IEVisible = inet.Visible
End FunctionNavigation Functions
These functions, respectively, allow you to tell IE to go back one page (according to History), go forward one page (according to History), or go to the Home page (as defined in Internet Options).
Function GoBack(inet As Object) inet.GoBack End Function Function GoForward(inet As Object) inet.GoForward End Function Function GoHome(inet As Object) inet.GoHome End Function
Navigate Method
Here is one of the more important methods — the one that lets you browse to a particular web page, and even submit POST data. Visit the Navigate Method for more details about each parameter. Usually I only use the URL parameter.
Function Navigate(inet As Object, url As String, flags As Variant, targetFrameName As Variant, _
postData As Variant, headers As Variant)
inet.Navigate url, flags, targetFrameName, postData, headers
End Function
Helper Functions
To close the IE instance, use the Quit Method.
Function IEQuit(inet As Object) inet.Quit End Function
Refresh the current page using the Refresh Method.
Function IERefresh(inet As Object) inet.Refresh End Function
To stop a page from loading, use the Stop Method.
Function IEStop(inet As Object) inet.Stop End Function
Next time we'll go through the HTMLDocument Object and, as a practical example, use the HTML Object Model to do something useful (extracting the UV Index from the U.S. EPA's SunWise website for a given zip code).
hi,
I am trying to set up a excel marcro witch copy "referencies" in an excel workbok and paste them into the search toll of a competitors web site and copy back to my sheet the price of the article.
I'v succeed the first step thanks to your tutoral but would like now to extract the price from the source new page:
ex: if the product exist, then copy the text betwens the tags
[cc lang=html]
9,40 €'< '/strong>
15,67 €/L
[/vb]
like this 9,40 and paste it in my excel.woorksheet
could you help me please
Excelent. Thank you.
I wish my VBA to look at: http://www.rac.co.uk/route-planner/?daddress=DE55%205BL
entering the "From: " and "To: " values from two columns of values (from my EXCEL worksheet) and setting other optional values ( Such as one way/ there and back ; Fastest or shortest ) from other columns to get the mileage between two distances in a column next to the columns in EXCEL.
If error, I wish the VBA macro to go to next pair of From/To values without stopping until the last pair of values.
I need thos repeating access for roughly 10000 lines.
Could you please help me?
You need more help than I can provide in a blog comment. Try posting this on http://www.vbaexpress.com or http://www.mrexcel.com. You might also want to show some starting code to increase your chances of getting help, because few people will want to write the entire code for you.
Thank you so much for posting this. Its nearly impossible to find info on this in any of the VBA books I've seen.
How would you use VBA to download a file from a website? I can get to the point where it clicks on the file to save, but then it brings up the save, open, close dialogue box. How would you get VBA to tell it to save?
Check out the URLDownloadToFile API, here's an example of how to use it:
Use the URLDownloadToFile API function to download a file from a URL into a file
Hi folks, this is great stuff thanks. I have 2 questions, hopefully quite simple.
1. When I Set GetIE = CreateObject("InternetExplorer.Application") if I issue Public GetIE as Object, does it create a globally accessible IE object that I can use anywhere, or do I need to pass it everywhere i.e. dim locally?
2. Is there a neat way in Excel VBA to solve this problem? My application has 2 IE browser windows open and 2 tabs in each browser window. How can I select and activate each tab by title at will?
e.g.
Browser 1, TAB 1
Browser 1, TAB 2
Browser 2, TAB 1
Browser 2, TAB 2
Would appreciate the help, many thanks, Alex
1) If you declare GetIE at the top of the module, outside of any particular procedure, it will be available to any procedure in that module.
How do I declare a global variable in VBA?
2) I'm afraid there's no easy way to do this. I can't even find anything at the end of a search, although I only spent a few minutes searching. You're better off creating the instance yourself if possible.
Hi, I am New to VBA Code, I have Developed the VBA Based Application in Excel, In That Some Internet Based VBA Code i have Developed. In That Upto Loging to the Website I have Made After that I Want to Select the Particular Link After Login to the Website. How I have to Select the Link . (eg. Course >> PG Coure) i want to Select PG Course