Is very demanded using basic script to perform a repeated job, it can be updating your stock details on regularly or scraping personal information of your group members like Name, phone numbers, emails etc. In this article I will take you through a basic IE automation tour where you will learn basic steps to be performed to automate Internet Explorer. In this article I am doing followings with the help of code:
- Open Internet Explorer
- Navigate Google page
- Locate search text box and put “VBAOVERALL” in
- Click on search button
- Any Office (Excel, Word, PowerPoint, Outlook) application
- Internet Explorer version 8 or higher (not Edge)
Step 1: Insert a module and name it as “modIEAutomation“. Then Put a procedure name it “Scrapping“, you can choose name as per your wish.
Step 2: Open Google home page by typing in the browser as “https://www.google.com”
Step 3: Right click on search text box and click on Inspect button as shown below (this example refers Google Chrome)
A console opens with highlighting the code written behind to build the Text Control that we need to grab and automate. Look for the “name” attribute in the highlighted tag and copy its value and keep it a side.
Repeat the same action for Search button and capture the “name” attribute’s value and keep it a side.
Note: some sites are dynamic and you may not be able to find name attributes in the tag but you can try other attributes like Tag name itself or ClassName and so on.
Step 4: Open VBA editor and do followings:
- Navigate Tools menu
- Click on References… (refer figure 1.1)
- Check following libraries and click OK (refer figure 1.2)
- Microsoft HTML Object Library
- Microsoft Internet Controls
Microsoft HTML Library: offers various object to hold and manipulate HTML controls available on a web page like TextBox, List, Combobox, Table, Popups etc.
Microsoft Internet Controls: has capability to deal with Internet Windows like resize, minimize, maximize, open, close etc.
Step 5: As mentioned above steps following code will do the job for you:
Public Sub Scrapping() Dim IE As InternetExplorer Dim oDoc As HTMLDocument 'Instantiate IE Set IE = New InternetExplorer 'Visble Internet Explorer IE.Visible = True 'Navigate url IE.navigate "https:\\www.google.com" 'Wait until IE is loading Do While IE.readyState < 4 DoEvents Loop 'Get page into a document object Set oDoc = IE.document 'locate google search textbox oDoc.getElementsByName("q").Item(0).Value = "VBAOVERALL" 'Locate button to perform search Dim iElms As IHTMLElementCollection Set iElms = oDoc.getElementsByTagName("input") Dim elm As IHTMLElement 'Iterate each input tag and look for classname For Each elm In iElms If elm.className = "gNO89b" Then 'Click on btn elm.Click End If Next elm 'Close internet explorer IE.Quit End Sub
Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.