Do You want to have the same automation in web as in your Excel or SAP? In todays article I’m going to show You how to create web automation using good old buddy Internet Explorer.
Internet Explorer was on the top of browsers…
…almost 20years ago. Currently it is not supported since 2015…
Now You probably ask – why can’t we use any other browser than Internet Explorer? Good question, but the answer is simple. Internet Explorer is a Microsoft company product, so every Windows user has got it on his PC – at least for now. Additionally You can already find the library in the VBE References, so there will be no problem to begin the work with that.
If You want to start with the early binding, You’ll need 2 references. Set in the options – Microsoft Internet Controls for the InternetExplorer object and Microsoft HTML Object Library for HTMLDocument object.
Of course You can go with the late binding. Setting everything as Object variable is good, but it is easier to begin with early binding.
The main 2 variables in our code are InternetExplorer object for the application and HTMLDocument for the website.
Dim IE As InternetExplorer Dim doc As HTMLDocument
The third one, which is also needed at least is the string for the URL.
Dim URL As String
As the example website I took this site.
So having set the application and the URL
Set IE = New InternetExplorer Let URL = "https://www.simpleexcelvba.com/"
we can set the visibility of the application to see what is happening and go to specified URL in the IE object.
IE.Visible = True IE.navigate URL
To wait until the page loads we can use here the most common method in the Internet.
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Thanks to that the code will not go further until everything is loaded on site.
One last thing before scraping the website is to set the site – HTML document – to the variable.
Set doc = IE.Document
Scrape the website data
For the purpose of this article we will do the basic thing – use the site’s browser. To start scraping first of all right click on the specified website and choose Inspect.
After that You will see additional window, something like VBE Immediate & Watch window mixed together.
This console window is showing us the code, which is responsible for how website is shown.
Get those site elements
Now choose the Select element to inspect icon in the upper left corner of the console.
Then click on the SimpleExcelVBA browser on the main page.
Thanks to that the console will highlight the code line, which stands to showing the browser.
The best to use will be the name of this element – s.
doc.getElementsByName("s").Item(0).Value = "Connect to SAP"
To do the actual automate search we need to click magnifier icon next to the browser. Inspect this element same as I described that in browser case and the console will highlight the line.
In this situation we will use class as identifier.
Worth to remember
If the .Value property, .Click method or anything is not working try with that trick of .Item(0) like I did here!
Full search automation code
Getting all together we achieve just a simple example of how to do automated search on SimpleExcelVBA.
Option Explicit Sub automateIE() Dim IE As InternetExplorer Dim doc As HTMLDocument Dim URL As String Set IE = New InternetExplorer Let URL = "https://www.simpleexcelvba.com/" IE.Visible = True IE.navigate URL Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop Set doc = IE.Document doc.getElementsByName("s").Item(0).Value = "Connect to SAP" doc.getElementsByClassName("search-submit").Item(0).Click End Sub
I hope that from now on, using this example, You will be able to start web automation using Internet Explorer!