This article contains an example of using late binding to create a new instance of Internet Explorer.
Please note that you can copy these code examples into any application that supports VBA, but the called application must be installed on your system for it to work. Just copy them into a standard module and run them. (No references need to be set in the Visual Basic Editor in order to make this code work.)
Create a New instance of Internet Explorer:
This code will create a new instance of Internet Explorer and bind to it. It will then navigate to the ExcelGuru home page.
Public Sub CreateInternetExplorer() 'Author : Ken Puls (www.excelguru.ca) 'Create an instance of Microsoft Internet Explorer Dim objApp As Object 'Create instance and set to visible On Error GoTo ErrHandler Set objApp = CreateObject("InternetExplorer.Application") With objApp .Visible = True End With 'Add some text to the document With objApp .Navigate Url:="http://www.excelguru.ca/" End With ErrHandler: 'Release the object and resume normal error handling Set objApp = Nothing On Error GoTo 0 End Sub
You cannot use the GetObject method used for the rest of the applications to bind to an existing Internet Explorer instance. In order to do this, we need to reach to a different method using Microsoft Internet Controls:
Public Sub GetInternetExplorer() 'Edited/Annotated by: Ken Puls (www.excelguru.ca) 'IMPORTANT: Requires reference to Microsoft Internet Controls! 'Go to Tools --> References --> Microsoft Internet Controls Dim shellWins As SHDocVw.ShellWindows Dim explorer As SHDocVw.InternetExplorer Set shellWins = New SHDocVw.ShellWindows For Each explorer In shellWins If explorer.Name = "Internet Explorer" Then Debug.Print explorer.LocationURL Debug.Print explorer.LocationName End If Next Set shellWins = Nothing Set explorer = Nothing End Sub
Rate this article