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
Alternatively, you can skip the referencing and use late binding, defining the pointer to the Internet Explorer instance as
SHDocVw.InternetExplorerin 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.
The next function loads a webpage:
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
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:
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.
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
InternetExplorer, and you'll find a list and short description of all methods, properties and events of this class.