Controls an instance of Windows Internet Explorer through automation.
The InternetExplorer object has these types of members:
The InternetExplorer object has these events.
Fires before navigation occurs in the given object (on either a window or frameset element).
Fires when the enabled state of a command changes.
Fires when a document is completely loaded and initialized.
Fires when a navigation operation begins.
Fires when a navigation operation finishes, is halted, or fails.
Fires after a navigation to a link is completed on either a window element or a frameSet element.
Fires after a navigation to a link is completed on a window element or a frameSet element.
Creates a new process to handle the navigation.
Fires when a new window is to be created.
Fires when a new window is to be created.
Raised when a new window is to be created. Extends NewWindow2 with additional information about the new window.
Fires when the FullScreen property is changed.
Fires when the MenuBar property is changed.
Fires before the Internet Explorer application quits.
Fires when the StatusBar property is changed.
Fires when the TheaterMode property is changed.
Fires when the ToolBar property is changed.
Fires when the Visible property of the object is changed.
Fires when a print template is instantiated.
Fires when a print template is destroyed.
Fires when the progress of a download operation is updated on the object.
Fires when the PutProperty method of the object changes the value of a property.
Fires when the status bar text of the object has changed.
Fires when the title of a document in the object becomes available or changes.
Fires when the visibility state of a content window, such as the browser window or a tab, changes.
The InternetExplorer object has these methods.
Executes a command and returns the status of the command execution using the IOleCommandTarget interface.
Gets the value associated with a user-defined property name.
Navigates backward one item in the history list.
Navigates forward one item in the history list.
Navigates to the current home or start page.
Navigates to the current search page.
Navigates to a resource identified by a URL or to a file identified by a full path.
Navigates the browser to a location that might not be expressed as a URL, such as a PIDL for an entity in the Windows Shell namespace.
Associates a user-defined name/value pair with the object.
Queries the object for the status of commands using the IOleCommandTarget interface.
Reloads the file that is currently displayed in the object.
Reloads the file that is currently displayed with the specified refresh level.
Cancels a pending navigation or download, and stops dynamic page elements, such as background sounds and animations.
The InternetExplorer object has these properties.
Sets or gets a value indicating whether the address bar of the object is visible or hidden.
Gets the automation object for the application that is hosting the WebBrowser Control.
Gets a value that indicates whether the object is engaged in a navigation or downloading operation.
Gets an object reference to a container.
Gets the automation object of the active document, if any.
Retrieves the fully qualified path of the Internet Explorer executable.
Sets or gets a value that indicates whether Internet Explorer is in full-screen mode or normal window mode.
Sets or gets the height of the object.
Sets or gets the coordinate of the left edge of the object.
Retrieves the path or title of the resource that is currently displayed.
Gets the URL of the resource that is currently displayed.
Sets or gets a value that indicates whether the Internet Explorer menu bar is visible.
Sets or gets a value that indicates whether the object is operating in offline mode.
Gets the parent of the object.
Retrieves the system folder of the Internet Explorer executable.
Gets the ready state of the object.
Sets or gets a value that indicates whether the object is registered as a top-level browser window.
Sets or gets a value that indicates whether the object is registered as a drop target for navigation.
Sets or gets a value that indicates whether the object can be resized.
Sets or gets a value that indicates whether the object can display dialog boxes.
Sets or gets a value that indicates whether the status bar for the object is visible.
Sets or gets whether the object is in theater mode.
Sets or gets whether toolbars for the object are visible.
Sets or gets the coordinate of the top edge of the object.
Gets a value that indicates whether the object is a top-level container.
Gets the user type name of the contained document object.
Sets or gets a value that indicates whether the object is visible or hidden.
Sets or gets the width of the object.
Windows Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.
The following example uses CreateObject in Visual Basic to launch an instance of Internet Explorer.
The following C# example launches an instance of Internet Explorer and navigates to a Web page. The code also demonstrates how to create an event handler to listen for the BeforeNavigate2 event. The project requires a reference to the Microsoft Internet Controls (SHDocVw) type library.
internetexplorer.application hangs on readystate=1 in VBA
I have an Excel VBA application which uses internetexplorer.application to explore an application. Starting around 2/21/15, this application started failing on readyState being stuck on 1 forever rather than ultimately migrating to 4. This only occurred when navigating the second (or further) link. The first URL works fine.
The machine with the problem is a Core i5 M520 CPU (4 CPU) running 64-bit Windows 7.
Another machine with a Core 2 Duo T9400 running 32-bit Windows 7 executes the code without a problem.
This feels like some sort of race condition but I am not sure if I have to do something special under 64-bit windows.
I am using Internet Explorer 11, Windows 7, and Excel 2003 (or 2013). Any idea on what is going wrong?
3 Answers 3
«Funny thing», sometimes the loop objIE.readyState <> 4 works, sometimes it doesn’t and hangs with readyState = 1 ( READYSTATE_LOADING ) even though the browser window is obviously finished loading.
It works best if I add a Sleep/DoEvents-loop (>500ms) after navigate and before doing anything else with the IE-object:
Hope that helps!
I’d say all the next as a comment, but have no comment privilige.
(1) You say that it’s stuck on readystate 1. Perhaps a silly question, but, how do you actually know that?
(2) It helps to disable irrelevent things when debugging. So comment-out the PeruseTheDocument call, and try again (on the off-chance that it’s something to do with that procedure).
(3) Not the cause of your problem, but: while a document is loading, IE.BUSY is TRUE for most but not all of the time. It actually does go FALSE occasionally before the document has fully loaded. So waiting on IE.BUSY=FALSE is generally not correct. In your case that won’t make any difference, because then you wait for readystate 4. But I’d remove the BUSY check, since it serves absolutely no purpose whatever, and it’s based on a misunderstanding of what that property actually means.
(Geez this interface makes things hard!! I have to make this an answer, because it’s too long for a comment.)
Your problem doesn’t occur for me.
I don’t have access to VBA, so I converted your code to VBScript. The difference is irrelevant to the issue at hand. Copy & paste my code into a file with .VBS extension. Double-click to run, and you’ll see each page go ready in turn.
Note that VBScript variables are declared untyped, never eg. «As Object». I also made objIE local to procedure Test_IE_Interface(), then pass it explicitly to LoadAPage() — that’s better design than using a global (but irrelevant to the problen at hand).
My conclusion is that either (1) you’re inadvertantly not actually showing us the actual code that is actually running; or (2) there’s something wrong with your network stack or internet connectivity. Personally I doubt it is (2).
Try my VBS code, see if it works, then convert it back to VBA and see what happens.
Internetexplorer application document
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 .
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.
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 Nothing .
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:
(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
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 SHDocVw , class InternetExplorer , and you’ll find a list and short description of all methods, properties and events of this class.