Excel vba for internet explorer

In this Article

  • Navigate to a Webpage with VBA
  • VBA Coding Made Easy
  • Open URL and Enter Data in Form Using VBA
  • GetElement in IE using VBA
  • Interact with IE using VBA
  • Sendkeys to Internet Explorer
  • Run Internet Explorer in Background
    • Selenium & VBA

This page contains coding examples for automating Internet Explorer (IE) using VBA.

** Update 6/7/2019: Currently, the best way to achieve web automation with VBA is by using Selenium. This article DOES NOT cover Selenium. The examples below will work, and might be sufficient for your needs. However, if you have more advanced needs or want to become an expert with web automation, I strongly recommend using Selenium instead. Dan Strong’s course on Web Automation (discount available through this link) is a fantastic resource to learn Selenium:

vba selenium web automation

(I receive an Affiliate Commission from Dan’s course)

Navigate to a Webpage with VBA

The First piece of code opens IE and navigates to a website. The second piece of code opens IE, navigates to a website and interacts with an input box.

Sub Automate_IE_Load_Page()
'This will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
 
    'Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
 
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True
 
    'Define URL
    URL = "https://www.automateexcel.com/excel/"
 
    'Navigate to URL
    IE.Navigate URL
 
    ' Statusbar let's user know website is loading
    Application.StatusBar = URL & " is loading. Please wait..."
 
    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
    Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until
 
    'Webpage Loaded
    Application.StatusBar = URL & " Loaded"
    
    'Unload IE
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
    
End Sub

A very common problem people encounter when working with IE in VBA is VBA attempting to run code before Internet Explorer has fully loaded. By using this code, you tell VBA to repeat a loop until IE is ready (IE.ReadyState – 4).

    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
    Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until

Also, note this line of code:

IE.Visible = TRUE

This code toggles whether IE runs in background or in the foreground.

VBA Coding Made Easy

Stop searching for VBA code online. Use the Code VBA Add-in to quickly insert your desired code into the Visual Basic Editor.

alt text
Learn More!

Open URL and Enter Data in Form Using VBA

'This Must go at the top of your module. It's used to set IE as the active window
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim HWNDSrc As Long
    
 
    'Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
 
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True
 
    'Define URL
    URL = "https://www.automateexcel.com/vba"
 
    'Navigate to URL
    IE.Navigate URL
 
    ' Statusbar let's user know website is loading
    Application.StatusBar = URL & " is loading. Please wait..."
 
    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop
 
    'Webpage Loaded
    Application.StatusBar = URL & " Loaded"
    
    'Get Window ID for IE so we can set it as activate window
    HWNDSrc = IE.HWND
    'Set IE as Active Window
    SetForegroundWindow HWNDSrc
    
    
    'Find & Fill Out Input Box
    n = 0
    
    For Each itm In IE.document.all
        If itm = "[object HTMLInputElement]" Then
        n = n + 1
            If n = 3 Then
                itm.Value = "orksheet"
                itm.Focus                             'Activates the Input box (makes the cursor appear)
                Application.SendKeys "{w}", True      'Simulates a 'W' keystroke. True tells VBA to wait
                                                      'until keystroke has finished before proceeding, allowing
                                                      'javascript on page to run and filter the table
                GoTo endmacro
            End If
        End If
    Next
    
    'Unload IE
endmacro:
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
    
End Sub

GetElement in IE using VBA

Interacting with objects in Internet Explorer can often be a pain. You need to identify what specific object to work with. In the above code, we are looking for the 3rd instance of “[object HTMLInputElement]” (an input form). Then we enter “orksheet” into the input form (itm.value = “orksheet”), move the cursor into the input form (itm.focus), and type “w”. Typing “w” is necessary in this instance to activate the javascript used to filter the table.

There are more direct methods to selecting objects, however this method should work if all else fails.

To use those other methods you will want to use the following options:

IE.document.getelementbyid("ID").value = "value"       'Find by ID
IE.document.getelementsbytagname("ID").value = "value"        'Find by tag
IE.document.getelementsbyclassname("ID").value = "value"      'Find by class
IE.document.getelementsbyname("ID").value = "value"           'Find by name

You can run into problems when using these methods if there are more than one element with the same name. By using a loop (as in the sample code above), you can specify which instance of the element to use.

Interact with IE using VBA

In the code above we use the event: Focus (itm.focus) to activate the cursor in the form.

You can find more examples of Object / Element Events, Methods, and Properties here: https://msdn.microsoft.com/en-us/library/ms535893(v=vs.85).aspx

Not all of these will work with every object / element and there may be quite a bit of trial and error when interacting with objects in IE.

Sendkeys to Internet Explorer

We used the Sendkeys command in the code above:

Application.SendKeys "{w}", True

Sendkeys should generally be a last resort. You should usually be able to interact with objects directly, however sometimes it’s easier to just use the Sendkeys command. Sendkeys is essentially the same as typing with the keyboard. You need to make sure the correct windows and objects are selecting before proceeding. Sendkeys can also trigger events that run based on user interaction on the web. In the example above, we use Sendkeys to activate the Javascript filter in the Table that we use on the web page.

Sendkeys has two inputs:
1. the key to enter (generally surrounded by {}… {enter}, {q}….)
2. Wait until Sendkeys has completed before proceeding TRUE/FALSE. You will generally want this set to TRUE when working with Internet Explorer.

Run Internet Explorer in Background

To run Internet Explorer in the background you need to do two things:

1. Call the macro containing the IE code with Application.Run so the macro runs in the background as you continue working:

Application.Run ("Automate_IE_Load_Page")

Note: This code could potentially interrupt your work, or your work could interfere with the code. For example, if you use SendKeys, Sendkeys may send a keystroke to the wrong application. Be very careful with this.
2. Hide IE:

IE.Visible = False

Selenium & VBA

If you found this article useful, you might want to check out Dan Strong’s course on Web Automation. It covers how to use Selenium with VBA.

vba selenium web automation

(I receive an Affiliate Commission from Dan’s course)

Testimonial from one of Dan’s students

Manostaxx

The text that follows is owned by the site above referred.

Here is only a small part of the article, for more please follow the link

SOURCE: http://automatetheweb.net

Learn to write Excel VBA code to automate your web browser.

Excel = Microsoft Excel
VBA = Visual Basic for Applications, the macro programming language within Excel
IE = Internet Explorer

Excel VBA Internet Explorer interaction

Most browser automation tools are complex, programmed by software engineers. But because XL and IE –both Microsoft products– play so well together, normal folks like you and I can build some pretty cool things too.

Web browsing & interaction, sending & replying to emails, social media interaction, blogging/posting/content editing, product searching/comparing/price-checking/buying, all kinds of stuff … you can build a robot to do it for you, and do it faster, and around the clock.

TL;DR »

Let’s start by building a basic web bot to open a search page, submit a search request, evaluate and scrape the results page, and return the results to Excel.

  1. Excel Basics – but first, if you’re new to Excel you should start here.
  2. VBA Basics – it would also help to know some VBA fundamentals (eg. what’s a variable, what’s a subroutine, for-next loops, the VBA Editor, etc) — these courses are good.
  3. HTML and the DOM – and for creating web-bots, you should understand basic HTML structure – how a webpage is laid out – the Document Object Model.

Ok, you’re grounded in the basics. Now let’s jump into building a simple bot to search for a keyword and return the results…

A beginner web automation project: keyword search bot

Let’s create a web bot that goes to my new favorite search engine, enters a keyword, submits the query, then scans the results and prints them to an Excel sheet, highlighting any links from yellowpages.com.

Begin by opening Excel and start a new, blank workbook.

Enter a keyword – let’s say auto parts – in cell A2, and a location – let’s say Houston TX – in cell C1.

Excel workbook with Developer tab selected

Excel workbook with Developer tab selected

Click the Developer tab (not showing?), and click Visual Basic.

Then click ToolsReferences, and add the reference libraries Microsoft HTML Object Library and Microsoft Internet Controls

Next, in the Visual Basic Editor (VBE), click ViewProject Explorer.

Then, right-click VBAProject, and Insert › a new Module

VBA insert new module

VBA – inserting a new module

Now copy & paste the code below into Module1

'start a new subroutine called SearchBot
Sub SearchBot()
 
    'dimension (declare or set aside memory for) our variables
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result as String 'string variable that will hold our result link
 
    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer
 
    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True 
 
    'navigate IE to this web page (a pretty neat search engine really) 
    objIE.navigate "https://duckduckgo.com" 
 
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 
 
    'in the search box put cell "A2" value, the word "in" and cell "C1" value 
    objIE.document.getElementById("search_form_input_homepage").Value = _ 
      Sheets("Sheet1").Range("A2").Value & " in " & Sheets("Sheet1").Range("C1").Value
 
    'click the 'go' button 
    objIE.document.getElementById("search_button_homepage").Click
 
    'wait again for the browser 
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 
 
    'the first search result will go in row 2 
    y = 2 
 
    'for each <a> element in the collection of objects with class of 'result__a'... 
    For Each aEle In objIE.document.getElementsByClassName("result__a") 
 
        '...get the href link and print it to the sheet in col C, row y 
        result = aEle
        Sheets("Sheet1").Range("C" & y).Value = result
 
        '...get the text within the element and print it to the sheet in col D
        Sheets("Sheet1").Range("D" & y).Value = aEle.innerText
        Debug.Print aEle.innerText
 
        'is it a yellowpages link?
        If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
            'make the result red
            Sheets("Sheet1").Range("C" & y).Interior.ColorIndex = 3
            'place a 1 to the left
            Sheets("Sheet1").Range("B" & y).Value = 1
        End If
 
        'increment our row counter, so the next result goes below 
        y = y + 1 
 
    'repeat times the # of ele's we have in the collection 
    Next
 
    'add up the yellowpages listings
    Sheets("Sheet1").Range("B1").Value = _
      Application.WorksheetFunction.Sum(Sheets("Sheet1").Range("B2:B100"))
 
    'close the browser
    objIE.Quit
 
'exit our SearchBot subroutine
End Sub

Your module should look like the image below. To run this subroutine you’d press the green play button or just press F5. But first, let’s have a closer look. Click the first line, Sub SearchBot(). Then press F8 to begin stepping through the code line-by-line (also called debugging).

VBA code module in VB Editor

VBA code module in VB Editor

Each time you press F8 the line of code highlighted in yellow will execute, and then the next line down of code will turn yellow and wait for your key press. Say you change the code in a line and want to re-run it… you’d move the yellow line back up by dragging the yellow arrow with your mouse, or by clicking the line you want and then pressing CTRL+F9

Notice that after executing the line objIE.Visible = True, Internet Explorer opens? Now position all 3 applications (Excel, VBE, and IE) on the screen so that each is visible, and continue stepping through the code using F8.

What’s happening here…

Let’s look at the code line by line. Watch the video too if you want.

The first thing you’ll notice are lots of comments I added to the code. VBA ignores lines beginning with a single quote ('), highlighting them green, as comments. Adding your own personal comments to your code is an especially good idea for beginners, or for more complex code that requires a memory-jogger when you go back to it.

Sub SearchBot()

This tells VBA we are starting a new subroutine, or Procedure. A module can contain subroutines (SubEnd Sub) and functions (FunctionEnd Function). In the video, we start the subroutine by putting the cursor within the code and pressing F5 (run) or F8 (execute line-by-line). But usually a Sub or Function is called by another Sub or Function, or by a launch button. The name of a Sub or Function can be almost anything you want. CamelCase naming convention, with first letters capitalized, is typical for naming subroutines and functions.

Dim objIE as InternetExplorer

Think of variables (like objIE) as floating references or containers that refer to objects or numbers we want to manipulate. But before we get to use them, we have to declare, or Dim, them, which sets aside some memory for whatever kind of use we have in mind for them. To manipulate Internet Explorer as an object in VBA, we need to Dim the browser — either generically as an Object variable (an example of late binding objects), or more specifically as an InternetExplorer special object variable like we did here (an example of early binding objects).

VBA Intellisense

VBA Intellisense code helper

Right now don’t worry about early vs. late binding… there are pros and cons for each method… but one advantage of early binding is VBA knows exactly what you intend for the variable, and therefore can show you its Intellisense (helper hints) popup menu when typing code after a specified object variable, which is great for helping beginners snoop around and see different things the object could possibly do. Why call it objIE? No special reason, aside from making it easy to remember what it’s for. Names of variables can be almost anything you want, and camelCase with first letter lowercase is the usual naming convention.

Dim aEle As HTMLLinkElement

Here we reserve aEle as a different type of object variable. Specifically, we early bind it for use as a webpage link (the <a> tags on a webpage). The other way would be to declare it more generically as Dim aEle as Object then later put it to use in one or more specific roles as an object — either way works. Once again, I could choose to call it anything, even mySuperHrefThingy, but I like the simplicity of aEle because it’s short and makes me think <a> element.

Dim y as Integer

Let’s use the variable y to refer to the row number on the Excel sheet where we’ll have the bot put our search results. Integer type variables in VBA are round numbers between -32,768 and 32,768. If we needed a variable to hold a value greater than 32,768 we’d Dim y as Long instead. Or if we needed it to have decimals we’d use Dim y as Double.

Dim result as String

For each aEle we find, we’ll copy its href value over to a string (alphanumeric text) variable called result, then print the string to the Excel sheet. Ok, I admit we don’t really need this –we could just print aEle’s value to the sheet directly– but I added it to introduce you to string variables.

Set objIE = New InternetExplorer

Setting an object instantiates or activates a new instance of its object type. So we’re saying let’s start a new instance of the IE browser.

objIE.Visible = True

Awesome – your bot just opened your IE browser! Properties (characteristics, such as making IE Visible) and Methods (actions) are invoked after typing a dot (.) after an object, like you see here. In your VBE, delete this line and start retyping it. You’ll notice after typing objIE. that the Intellisense helper pops up, listing all the properties and methods available for the InternetExplorer object. Go ahead and scroll through the list.

objIE.navigate "https://duckduckgo.com"

The .navigate event tells IE to navigate to whatever webpage is within the quotes. Yay automation time!

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

But the code wants to run all at once, in a millisecond. By the time IE actually loads the page of search results, the entire subroutine is long over. We have to slow things down and wait for the browser while its navigating. We could use something like Application.Wait(Now + TimeValue("00:00:05")) to wait 5 seconds, but if the page loads faster we’re wasting time waiting for nothing, and if the page takes longer we’re in the same pickle as before. Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop is an elegant Do...Loopsolution consisting of 3 VBA statements we’ve tidied up on one line, using colons (:) to connect them. It’s saying: while IE is busy navigating, just sit here and run in circles. objIE.Busy = True and objIE.readyState <> 4 (<>means not equal) are essentially the same thing –one could probably be ommitted– but just to make sure the page is really done loading we make both of these conditions requirements for getting out of the loop, by using the logical operator Or. The DoEvents part is technically not needed but we add it in the loop to free up processing resources and allow other things to happen within Excel during this time, asynchronous to the running loop. For the over-achievers out there, here are some other ways we could have written this line of code to do the same thing.

objIE.document.getElementById("search_form_input_homepage").Value = _
Sheets("Sheet1").Range("A2").Value & " in " & Sheets("Sheet1").Range("C1").Value

This VBA statement says: Find the search box element on the webpage by its ID, which is search_form_input_homepage, and make its text value what we have in cell A2, plus the string ” in “, plus what we have in cell C1. The underscore (_) just lets us put longer pieces of code on multiple lines for better readability. How did I know what the search input box’s ID was, or that it even had an ID? Firebug of course! If you don’t have Firefox get it, and then install the Firebug add-on. Then right-click the search box, click Inspect Element with Firebug, and behold…

Firefox Firebug Inspector

Firefox Firebug Inspector
Firebug Console - highlighted code
Firebug Console – highlighted code

…the line of code is highlighted in the Firebug console (I added the red box around the ID). The image above reminds us a webpage is organized in a nested, tree-like structure, starting with .document as the root objectand branching out from there into smaller objects. This is called the Document Object Model. .getElementById("__") is the go-to tool for grabbing one of those objects from the webpage. With it you can scrape data from a page, as well as writing to a webpage as we’re doing here. Here’s a sneak peak at some other ways we could have found the search box and entered data into it (shown in the order I would have tried them, if an ID was not available).

objIE.document.getElementById("search_button_homepage").Click

I used Firebug to find the ID of the search button (magnifying glass) on the page, then used .getElementByIdagain to grab it, and the .Click method to click it. Alternatively, we could have submitted the search box form like this instead objIE.document.getElementById("search_form_homepage").Submit. Actually, there’s a simpler method still that would allow us to skip the previous 3 steps altogether. Can you see it? Hint: take a look at the url we ended up with… https://duckduckgo.com/?q=auto+parts+in+Houston+TX. Got it? If you still don’t see it, and you don’t think you’ll be able to sleep tonight, email me ?

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

We need to wait for the browser for a second (an eternity in CPU land) while the search is running.

y = 2

Now we have a page full of search results. We’ll put them in column C, but we need to start in row 2 since C1 contains our heading Houston TX.

For Each aEle In objIE.document.getElementsByClassName("result__a")

This is the opening statement in our action-packed For...Next loop, where the procedure seeks out each aEle(link) object that’s found in the collection of all objects on the page that have a class of result__a (again I used Firebug to locate this particular class name to target). For each match found, we temporarily assign it to the aEle variable and do the following with it…

result = aEle
Sheets("Sheet1").Range("C" & y).Value = result

We take the found <a> element (aEle) and assign its value to a string variable, result. Ok, we really didn’t need to do this… we could just work with aEle directly without passing it around… I just wanted to demonstrate what a string variable is! Then we print the string to the sheet in column C, row y.

Sheets("Sheet1").Range("D" & y).Value = aEle.innerText

Let’s say we also grab the anchor text for each link and plop that into the cell to the right. The .innerTextproperty (or .textContent) gets us that text. Side-note: I’ve built many bots with the code If InStr(objIE.document.body.innerText, "some text") > 0 then ... where I needed to know if the web page had some text in it somewhere.

Debug.Print aEle.innerText

We’ll also print this text in the debugging console, the Immediate Window, as well (note: you may have to press CTRL+G to show the Immediate Window, or open it from the View menu).

If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
Sheets("Sheet1").Range("C" & y).Interior.ColorIndex = 3
Sheets("Sheet1").Range("B" & y).Value = 1
End If

Let’s say we want to single out Yellow Pages links. InStr looks for one string inside another, using the format InStr(haystack, needle) and returns the numerical starting point of the first occurence of needle, or 0 if not found. If ‘yp’ or ‘yellowpages’ is found in a link we make the interior of the cell red with the .ColorIndex VBA property, and place a ‘1’ in the cell to the left. FYI, short and simple If...Then statements, like this one: If x = y Then Debug.print "Yo, x is the same as y, bro!", can be written on one line with the closing End Ifommitted. If...Then statements are the backbone of any great A.I. … Watson, Wall-E, and now SearchBot()!

y = y + 1

Now we increment y by 1, so each new result found will be printed on the next row down.

Next

Continue looping until we run out of aEle‘s to look at.

Sheets("Sheet1").Range("B1").Value = _
Application.WorksheetFunction.Sum(Sheets("Sheet1").Range("B2:B100"))

Since we put a 1 in row B each time we found a Yellowpages link, we can now just add them up, making cell B1 display the total. Alternatively, we could have used another For...Next loop to look at each cell in column B one at a time, adding up all the 1’s. But using Excel’s powerful built-in WorksheetFunction‘s whenever possible is better.

objIE.Quit
End Sub

So we opened IE, navigated to DuckDuckGo, performed a search, parsed the results on the web page, printed them to Excel, and even did some further analysis on the retrieved data. All that’s left is to close (.Quit) the browser object and end (End Sub) the procedure.

Finally, let’s save the file. We need to save the file with the extension .xlsm, since this is most definitely a macro-enabled workbook.

Excel - Save-as .xlsm

Excel – Save-as .xlsm

Congratulations! You built your first bot, and in the process learned a good deal about the most common VBA commands used to interact with and automate the web.

To download this Excel VBA project: xl-searchbot.xlsm (keyword search & scrape bot)

Recommended next: Web scraping with getElementsByTagName()

Write a macro in VBA to fill in an IE form (Internet Explorer)

You can use VBA to talk to Internet Explorer, providing that you understand the structure of the web page you’re talking to. This blog shows how to do this!

This is part of our full Excel VBA tutorial — Wise Owl also run courses in Excel, as well as classroom training in VBA programming.

Posted by
Andy Brown
on 02 November 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

I’ve shamelessly stolen my colleague Andrew’s idea to create this blog,
showing how to fill in an Internet Explorer (IE) form from within VBA. 
I’ve also shamelessly borrowed ideas from Fergus Cairns — thanks, Fergus.

Completing IE form

We’ll create a macro in VBA to fill in the search form on the
old Wise Owl site (now superseded, but the principle remains the
same).

Note that our website has changed since this blog was written, so while the
principle of the code shown below is still good, it won’t work on our site. 
I’ve published a newer blog on the same subject here
— or you can see a full list of all of our VBA training resources
here.

Understanding the Target Page

The first thing to do is to know what the elements on the target website page
are called.  To do this, load the website in IE and view its source HTML:

Viewing source HTML in IE

In Internet Explorer 9, this is how you view
source HTML using the right mouse button menu.

If you press CTRL + F to find
some known text (eg Search for this page), you should be able —
eventually — to track down the part of the form of interest:

The HTML for the search button

The HTML for the search button on the Wise Owl website.

See the end of this blog for the BBC and Google UK search form field names.

From the above we can see that the search form with id search contains two
controls which a user can interact with:

  1. A textbox called SearchBox; and
  2. A button called submit2.

Now that we know what we’re trying to look to in the Document Object
Model
, we can start writing our VBA.

Referencing the Microsoft Internet Controls

The next thing to do is to make sure that we can get VBA to talk to Internet
Explorer.  To do this, reference the object library called
Microsoft Internet Controls
(if you’re not sure what references are, read this
first).

First, in VBA create a reference:

Tools / References menu

Choose the option shown from the VBA code editor (this example is for Excel).

Select Microsoft Internet Controls (you may find it helpful to
type an M in first, to go down to the object libraries starting
with M):

The Microsoft Internet Controls object library

Tick the
Microsoft Internet Controls object library reference

Select OK.  If you choose the menu option
again, you’ll see this reference has moved to near the top of the list:

References being used in VBA

The libraries referenced always include Excel. The one you just chose appears near the top of the list.

The Code to Link to IE

You can now write the code to link to Internet Explorer — here’s a
suggestion:

Sub UseInternetExplorer()

Dim ieApp As New SHDocVw.InternetExplorer

ieApp.Visible = True

ieApp.Navigate «Wise Owl or other website address goes here»

Do While ieApp.busy

Loop

Dim ieElement As Object

Set ieElement = ieApp.document.getElementByID(«search»)

ieElement(0).Value = «Excel VBA courses»

ieElement(1).Click

End Sub

If you run this macro, it should search the Wise Owl site for information on
Excel VBA courses!

Except that it might not — timing is everything.  You may need to delay
your code to wait for Internet Explorer or your website to catch up — see below.

Delaying your code

Unfortunately, code runs really quickly and websites load slowly (at least
relatively), so you often need to slow your code down with loops like this:

Do While ieApp.Busy

DoEvents

Loop

Or possibly even this:

Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE

DoEvents

Loop

It seems to be more or less guesswork how many you put in!

BBC and Google Help

Fergus Cairns informs me that you can substitute the following terms for the BBC
website and Google UK:

Website Search box Find button
Google UK gbqfq btnG
BBC UK orb-search-q orb-search-button

So at the time of writing this (September 2014) the following code works —
usually!

Sub FillInBBCSearchForm()

Dim ieApp As New SHDocVw.InternetExplorer

ieApp.Visible = True

ieApp.Navigate «http://www.google.co.uk»

Do While ieApp.Busy

DoEvents

Loop

Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE

DoEvents

Loop

ieApp.Document.getElementById(«gbqfq»).Value = «Excel VBA»

Do While ieApp.Busy

DoEvents

Loop

Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE

DoEvents

Loop

ieApp.Document.all(«btnG»).Click

End Sub

Notice that I’ve put in a fairly random number of loops to slow this down!

This blog has 1 thread

Add post

26 Aug 19 at
08:52

Thank you so much for the excellent VBA posts and videos!

My question is this — is there no way to program VBA to just wait until IE is finished loading, and then execute the next line of code? What if the load time varies?

26 Aug 19 at
11:06

VBA only knows about what’s going on inside one Office application, so can’t detect events happening in Windows or a browser.  What you could probably do is to write a Windows API call to detect when an Internet Explorer thread has finished, but to be honest I don’t know if this is possible, nor if it is how to go about it!

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.

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 you’ll 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, we’ve showed you how to do some really cool stuff with VBA. For example, you can use it to send emails directly from inside Excel, you can automatically export Outlook tasks to an Excel spreadsheet, and you can even design your own Internet browser! It isn’t 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 the ObjectARX SDK for AutoCAD — there are ways to «plug into» more applications than you probably realize.

The Idea

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

What I’m 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 you’re 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 (I’m using 2013 — other versions are similar when it comes to VBA programming) and go to the Developer menu item. Inside there, you’ll see an insert button, which drops down all of your controls. Select the ActiveX pushbutton control and place it in your spreadsheet.

IE-automation1

Presumably, you’ve already created a header for URLs if you want, but you don’t have to. This is really a URL storage library, so headers don’t really matter. Once you add the button, double click on it to open up the VBA editor. To the lower left, you’ll 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.

save-urls

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.

IE-automation3

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

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

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.

IE-automation4

So, in theory if you’re 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.

IE-automation5

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

IE-automation6

One caveat. If you aren’t using a header row, then you’ll 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. Here’s what the cmdLoadURLs_Click() function should look like.

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 isn’t all that long or complicated. You create a new instance of IE, make it visible (this will open IE without loading an URL). Next it’ll load the first URL in the list.

IE-automation8

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 (that’s what the «CLng(2048)» does, until it hits a blank cell in your spreadsheet, then it’ll stop opening new tabs. Here’s my IE browser with all four original tabs recovered using the Excel IE automation script.

IE-automation9

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 time…and it’s 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!

Ok, Excel probably isn’t the first thing that comes to mind when needing to deal with web pages. But sometimes it’s necessary to access them from your Excel Application (or Word or any other MS Office Application). And it’s easier than you probably think.

To make the following code work, you’ll need to include the «Microsoft Internet Controls» library in your VBA references first.
Go to your Visual Basic Editor, Menu Tools -> References... and select the entry «Microsoft Internet Controls». If you can’t find it in the list of available references, search for a file named shdocvw.dll, usually to be found in your Windows directory, subfolder System32.

Add reference to 'Microsoft Internet Controls' library

Alternatively, you can skip the referencing and use late binding, defining the pointer to the Internet Explorer instance as Object instead of SHDocVw.InternetExplorer in your VBA code. But then you’ll miss out on a lot of goodies like intellisense, full access to the object model via the object browser, access to the application’s built-in constants, and a popup listing all the supported constants upon typing…

Before you can do anything with the Internet Explorer, of course you’ll need one, and you’ll need something to address it. The following function achieves exactly this by creating a new instance of the Internet Explorer and returning a pointer to it.

'returns new instance of Internet Explorer
Function GetNewIE() As SHDocVw.InternetExplorer
  'create new IE instance
  Set GetNewIE = New SHDocVw.InternetExplorer
  'start with a blank page
  GetNewIE.Navigate2 "about:Blank"
End Function

The next function loads a webpage:

'loads a web page and returns True or False depending on
'whether the page could be loaded or not
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, _
                     i_URL As String) As Boolean
  With i_IE
    'open page
    .Navigate i_URL
    'wait until IE finished loading the page
    Do While .ReadyState <> READYSTATE_COMPLETE
      Application.Wait Now + TimeValue("0:00:01")
    Loop
    'check if page could be loaded
    If .Document.URL = i_URL Then
      LoadWebPage = True
    End If
  End With
End Function

But what if the desired page has already been loaded and you just want to use it?
The following function returns a pointer to an already open instance of the Internet Explorer, if it has the desired page loaded. It’ll find it by comparing this page’s URL with the URLs of open pages in the Internet Explorer. If the page could not be found, the function returns Nothing.

'finds an open IE site by checking the URL
Function GetOpenIEByURL(ByVal i_URL As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByURL In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByURL.Document) = "HTMLDocument" Then
      'check the URL
      If GetOpenIEByURL.Document.URL = i_URL Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function

Sometimes, when you enter an URL to load a page, you’ll get redirected, and thus the URL changes. Or your site uses frames and has the same URL for different content, so you can’t clearly identify a page by its URL. Then you could check the title of the page instead:

'finds an open IE site by checking the title
Function GetOpenIEByTitle(i_Title As String, _
                          Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByTitle In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByTitle.Document) = "HTMLDocument" Then
      'check the title
      If GetOpenIEByTitle.Document.Title Like i_Title Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function

(This was tested with IE6. Since IE7 uses tabs for different webpages instead of different windows, this code probably won’t work for IE7.)

Now that you have your web page (either by loading it new, or by accessing an already opened page), you can extract information from it, or manipulate the document or do whatever you want/need to do.
To make work with html documents easier, the Document object itself is just the root of a hierarchy of objects (and arrays of objects) that represent the different html tags in the document with all their properties and content. So if you need to address a certain element, you wont have to parse the html source, but you can quite easily address the element directly by its name or index.

A good introduction about how to work with the DOM (Document Object Model) and how to use it with forms in particular is given on this German site: Tagmodell des Webbrowsers

Here’s a link to a full reference about the document object and all elements in there.

Last but not least, a very good and comprehensive but also German site is the SELFHTML DOM reference

And because an example often is easier to understand than long-winded explanations: the following example goes to Wikipedia, enters a search string and navigates to the search result.

Sub ExplorerTest()
Const myPageTitle As String = "Wikipedia"
Const myPageURL As String = "http://en.wikipedia.org/wiki/Main_Page"
Const mySearchForm As String = "searchform"
Const mySearchInput As String = "searchInput"
Const mySearchTerm As String = "Document Object Model"
Const myButton As String = "Go"

Dim myIE As SHDocVw.InternetExplorer

  'check if page is already open
  Set myIE = GetOpenIEByTitle(myPageTitle, False)
  
  If myIE Is Nothing Then
    'page isn't open yet
    'create new IE instance
    Set myIE = GetNewIE
    'make IE window visible
    myIE.Visible = True
    'load page
    If LoadWebPage(myIE, myPageURL) = False Then
      'page wasn't loaded
      MsgBox "Couldn't open page"
      Exit Sub
    End If
  End If
  
  With myIE.Document.forms(mySearchForm)
    'enter search term in text field
    .elements(mySearchInput).Value = mySearchTerm
    'press button "Go"
    .elements(myButton).Click
  End With
       
End Sub

Of course an Internet Explorer Object doesn’t only contain the Document object. There’s a lot more, and you can find an overview by going to your VBA editor and pressing F2. This will open the object browser, where you can look for library SHDocVw, class InternetExplorer, and you’ll find a list and short description of all methods, properties and events of this class.

Его интеграция с Windows позволяет несколькими удивительными способами управлять Internet Explorer, используя сценарий Visual Basic для приложений (VBA) из любого приложения, которое его поддерживает, например Word, Outlook или Excel.

Автоматизация VBA — особенно прямая автоматизация браузера, такого как IE, как вы увидите в этой статье, — это как раз то, что превращает VBA из удобного сценария программирования в мощный язык автоматизации. Что делает его таким удивительным, так это тот факт, что многие приложения с элементами управления или объектами создаются просто для того, чтобы позволить вам интегрироваться в них с использованием языка программирования VBA.

За прошедшие годы мы показали вам, как делать действительно классные вещи с VBA. Например, вы можете использовать его для отправки электронных писем непосредственно из Excel. отправлять электронные письма из , вы можете автоматически экспортировать задачи Outlook в электронную таблицу Excel. , и вы даже можете создать свой собственный интернет-браузер. ! Это не только продукты Microsoft. Существуют сторонние приложения от разных производителей, которые интегрировали VBA и совместимые объекты в свое программное обеспечение — от Adobe Acrobat SDK до ObjectARX SDK для AutoCAD — есть способы «подключить» больше приложений, чем вы, вероятно, представляете.

Идея

В этом случае вы собираетесь подключить Excel с IE. Почему IE? Поскольку Internet Explorer настолько хорошо интегрирован с операционной системой, что вам действительно не нужно много делать, чтобы начать использовать автоматизацию IE в VBA в других продуктах Microsoft, таких как Word или Excel. В этом вся прелесть. В этой статье вы увидите, как работает эта автоматизация, а в следующей статье вы узнаете, как сделать то же самое с другими браузерами.

Здесь я покажу вам, казалось бы, простое приложение, но в нем есть множество приложений, в которых вы можете использовать этот код для выполнения множества интересных вещей с помощью браузера. Суть в том, что вы собираетесь создать электронную таблицу Excel с целью быстрого сохранения всех открытых окон браузера одним нажатием кнопки. Вы можете сохранить эту таблицу и уйти или выключить компьютер.

Вернитесь через час или три дня назад, откройте электронную таблицу, нажмите другую кнопку, и эти сохраненные URL-адреса снова откроются на том же количестве вкладок, что и раньше. Очевидным классным вариантом использования этого было бы хранить целую библиотеку общих сетевых настроек рабочего пространства в Excel. Затем вы можете восстановить это рабочее пространство одним нажатием кнопки без необходимости повторного поиска всех этих URL-адресов.

Автоматизация Internet Explorer с помощью VBA

Первое, что нужно сделать, это открыть Excel (я использую 2013 — другие версии похожи в программировании на VBA) и перейти в пункт меню Developer. Внутри вы увидите кнопку вставки, которая опускает все ваши элементы управления. Выберите элемент управления ActiveX и поместите его в электронную таблицу.

IE-automation1

Предположительно, вы уже создали заголовок для URL, если хотите, но не обязаны. Это действительно библиотека хранения URL, поэтому заголовки не имеют большого значения. Как только вы добавите кнопку, дважды щелкните по ней, чтобы открыть редактор VBA. Внизу слева вы увидите свойства вашей новой кнопки.

Переименуйте его в что-то вроде cmdSaveURLs и установите для заголовка «Сохранить URL-адреса», указывая, что это кнопка для сохранения всех открытых URL-адресов из браузера IE.

Спаси URLs

Затем перейдите в меню «Сервис» в верхней части редактора VBA, нажмите «Ссылки» в меню и прокрутите длинный список, чтобы найти ссылку «Microsoft Internet Controls». Установите флажок слева от него и нажмите кнопку ОК.

IE-automation3

Теперь вы готовы к работе. В текстовой области редактора вы должны увидеть строку «Private Sub cmdSaveURLs_Click ()». Если вы этого не видите, щелкните левое раскрывающееся поле над текстовой областью и найдите cmdSaveURLs в списке. Выберите его, и он создаст функцию Click () для вас.

Это код, который вы хотите вставить в эту функцию:

  я IE как объект
 Dim shellWins As New ShellWindows
 Dim IE_TabURL As String
 Dim intRowPosition как целое число

 intRowPosition = 2

 Для каждого IE в shellWins
         IE_TabURL = IE.LocationURL
         Если IE_TabURL <> vbNullString Тогда
             Sheet1.Range ("A" & intRowPosition) = IE_TabURL
             intRowPosition = intRowPosition + 1
         End If

 следующий

 Установить shellWins = Nothing
 Установить IE = ничего 

Ссылка Microsoft Scripting Runtime позволяет вам получить доступ к объекту ShellWindows, который позволяет перебирать Windows и находить экземпляры IE, которые у вас открыты. Этот скрипт найдет все открытые вами URL-адреса и запишет их в электронную таблицу Excel.

IE-automation4

Таким образом, теоретически, если вы работаете над чем-то вроде ведения блога, и у вас есть несколько открытых элементов, таких как окна исследований, редактор блога или окно календаря — все эти вкладки будут активными. Если вам нужно завершить работу или уйти в спешке, может быть очень сложно сохранить свое местоположение, скопировав все эти URL-адреса.

IE-automation5

С новым скриптом Excel просто нажмите кнопку «Загрузить URL-адреса», и он загрузится прямо в электронную таблицу.

IE-automation6

Одно предостережение. Если вы не используете строку заголовка, вам нужно изменить строку «intRowPosition = 2» на «intRowPosition = 1», и это начнется с первой строки, а не с пропуска строки заголовка.

Открытие сохраненной рабочей области браузера

Следующий этап этого проекта — пойти в другом направлении. Нажмите «Загрузить URL-адреса», чтобы Excel запустил IE и перезагрузил все те URL-адреса, которые вы сохранили в электронной таблице. Вот как должна выглядеть функция cmdLoadURLs_Click ().

  Тусклый IE как объект
 Dim shellWins As New ShellWindows
 Dim IE_TabURL As String
 Dim intRowPosition как целое число

 intRowPosition = 2

 Установить IE = CreateObject ("InternetExplorer.Application")
 IE.Visible = True

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

 Пока IE.Busy
     DoEvents
 венед

 intRowPosition = intRowPosition + 1

 Хотя Sheet1.Range ("A" и intRowPosition) <> vbNullString
     IE.Navigate Sheet1.Range ("A" и intRowPosition), CLng (2048)

     Пока IE.Busy
         DoEvents
     венед

     intRowPosition = intRowPosition + 1
 венед

 Установить IE = ничего 

Здесь есть несколько шагов, но, как вы можете видеть, код не такой длинный и сложный. Вы создаете новый экземпляр IE, делаете его видимым (это откроет IE без загрузки URL). Затем он загрузит первый URL в списке.

IE-automation8

Часть сценария «Пока IE.Busy» ожидает, пока страница полностью не загрузится, а затем переходит к остальным URL-адресам в вашей электронной таблице, открывая новую вкладку (это то, что делает «CLng (2048)», пока он попадает в пустую ячейку вашей электронной таблицы, затем он прекращает открывать новые вкладки.Это мой браузер IE со всеми четырьмя исходными вкладками, восстановленными с помощью сценария автоматизации Excel IE.

IE-automation9

Резюме

Моей настоящей целью было сделать так, чтобы отдельные таблицы создавали наборы вкладок для таких задач, как исследование и запись в моем собственном блоге, запись в , выполнение SEO-проектов на сайте или полный список других ролей или проектов, которые требуют сохраненная коллекция вкладок, которые всегда используются.

Использование электронной таблицы для хранения этих настроек и автоматизация их открытия в браузере может сэкономить много времени … и на самом деле это тоже здорово.

Используете ли вы какой-либо вид автоматизации IE в своих приложениях VBA? Видите какие-нибудь другие интересные варианты использования такого элемента управления IE из Excel? Поделитесь своими мыслями и отзывами в разделе комментариев ниже!

What is Data Scraping?

Data scraping is the technique that helps in the extraction of desired information from a HTML web page to a local file present in your local machine. Normally, a local file could correspond to an excel file, word file, or to say any Microsoft office application. It helps in channeling critical information from the web page.

The data scraping becomes simple when working on a research-based project on a daily basis, and such a project is purely dependent on the internet and website. To further illustrate on the topic, let us take the example of a day trader who runs an excel macro for pulling market information from a finance website into an excel sheet using VBA.

In this tutorial, you will learn:

  • What is Data Scraping?
  • How to prepare Excel Macro before performing Data Scraping using Internet explorer?
  • How to Open Internet Explorer using Excel VBA?
  • How to Open Website in Internet explorer using VBA?
  • How to Scrape information from Website using VBA?

How to prepare Excel Macro before performing Data Scraping using Internet explorer?

There are certain prerequisites that has to be performed on the excel macro file before getting into the process of data scraping in excel.

These prerequisites are as follows: –

Step 1) Open an Excel-based Macro and access the developer option of excel.

Step 2) Select Visual Basic option under Developer ribbon.

Step 3) Insert a new module.

Step 4) Initialize a new subroutine

Sub test()
End sub

The module would result as follows: –

Step 5) Access the reference option under the tool tab and reference Microsoft HTML Object Library and Microsoft internet control.

The following files are to be referenced to the module as it helps in opening internet explorer and facilitates the development of macro scripting.

Now the Excel file is ready to interact with the internet explorer. The next step would be to incorporate macro scripts that would facilitate data scraping in HTML.

How to Open Internet Explorer using Excel VBA?

Step 1) Initialize the variable in the subroutines as displayed below

Sub test()
Dim ie As New InternetExplorer 
Dim doc As New HTMLDocument

Step 2) To open internet explorer using VBA, write i.e. visible=true and press F5.

Sub test()
Dim ie As New InternetExplorer 
Dim doc As New HTMLDocument
Ie.visible=true

The module would look as follows: –

How to Open Website in Internet explorer using VBA?

Here, are steps to Open Website in Internet exploer using VBA

Step 1) Once you are able to access the internet explorer using Excel VBA, the next step would incorporate the accessing of a website using VBA. This facilitated by Navigate Attribute, wherein the URL has to pass as double quotes in the attribute. Follow the following steps as displayed.

Sub test()
Dim, ie As New InternetExplorer
Dim doc As New HTMLDocument
Dim ecoll As Object
ie.Visible = True
ie.navigate"http://demo.guru99.com/test/web-table-element.php"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE

Step 2) – Press F5 to execute the macro. The following webpage would be opened as displayed

Now, the excel macro is ready with respect to performing the scraping functions. The next step would display how the information can be extracted from internet explorer using VBA.

How to Scrape information from Website using VBA?

Suppose the day trader wants to access the data from the website on a daily basis. Each time the day trader presses the click the button, it should auto pull the market data into excel.

From the above website, it would be necessary to inspect an element and observe how the data is structured.

Step 1) Access the below source code of HTML by pressing control + Shift + I

<table class="datatable">
<thead>
<tr>
<th>Company</th>
<th>Group</th>
<th>Pre Close (Rs)</th>
<th>Current Price (Rs)</th>
<th>% Change</th>
</tr>

The source code would be as follows: –

Sub test()
Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
Dim ecoll As Object
ie.Visible = True
ie.navigate "http://demo.guru99.com/test/web-table-element.php"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document

As it can be seen that the data is structured as a single HTML Table. Therefore, in order to pull entire data from the html table, it would require designing of macro which collects the data in the form of a collection.

The collection would then be pasted into excel. To achieve, the desired results perform the below-mentioned steps: –

Step 2) Initialize the Html document in the subroutine

The VBA module would look as follows: –

Step 3) Initialize the collection element present in the HTML document

The VBA module would look as follows: –

Sub test()
Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
Dim ecoll As Object
ie.Visible = True
ie.navigate "http://demo.guru99.com/test/web-table-element.php"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Set ecoll = doc.getElementsByTagName("table")

Step 4) Initialize the excel sheet cells with the help of nested loop as shown

The VBA module would look as follows: –

Sub test()
Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
Dim ecoll As Object

ie.Visible = True
ie.navigate "http://demo.guru99.com/test/web-table-element.php"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Set ecoll = doc.getElementsByTagName("table")

The excel can be initialized using the range attribute of the excel sheet or through cells attribute of the excel sheet. To reduce the complexity of the VBA script, the collection data is initialized to the excel cells attribute of sheet 1 present in the workbook.

Once the macro script is ready, pass and assign the subroutine to excel button and exit the module of VBA. Label the button as refresh or any suitable name that could be initialized to it. For this example, the button is initialized as a refresh.

Step 5) Press the refresh button to get the below-mentioned output

Step 6) Compare the results in excel with the results of internet explorer

Summary:

  • The data scraping allows the user to scrape out only the information that the user wants.
  • Scraping can be performed using internet explorer.
  • The process of scraping is slower in the case of internet explorer; however, it delivers the desired results to the user.
  • The scraping should be performed with absolute carefulness and caution as it can harm and crash the system being utilized for scraping.

Like this post? Please share to your friends:
  • Excel vba listbox filter
  • Excel vba for integer
  • Excel vba open all excel files
  • Excel vba listbox count
  • Excel vba for index