Attaching to an existing Internet Explorer instance

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Attaching to an existing Internet Explorer instance

Post by Marcus Tettmar » Wed May 07, 2008 9:49 am

There are various scripts on these forums demonstrating how to automate Internet Explorer via VBScript and IE's Document Object Model. Such as this one: http://www.mjtnet.com/forum/viewtopic.php?t=1461

These require that the script creates the Internet Explorer instance via a CreateObject call. The script then has access to the IE object and can access its methods and properties in order to automate navigation, clicking on links, filling forms and extracting data from the page, etc.

A question that has come up a few times is "How do I connect to an already running copy of IE, one that the script didn't start?". This is what the GetObject function is designed for, but Internet Explorer doesn't support it.

Well I recently discovered a solution. The following script has a subroutine called GetIE which will find a running copy of IE and set the global IE object variable to that copy. This can then be used in other functions to control that copy of IE:

Code: Select all

VBSTART

Dim IE

'Attaches to an existing instance of IE with matching URL
Sub GetIE(URL)
  Dim objInstances, objIE
  Set objInstances = CreateObject("Shell.Application").windows
  If objInstances.Count > 0 Then '/// make sure we have instances open.
    For Each objIE In objInstances
	  If InStr(objIE.LocationURL,URL) > 0 then
	    Set IE = objIE
	  End if
    Next
  End if
End Sub

'fills a form field and optionally submits form
Sub WebFormFill(fieldname,fieldvalue,submit)
  Dim FormNr
  Dim ItemNr
  Dim TheForm

  if IE.Document.All.Tags("FORM").Length = 0 then
    MsgBox("No form found in page")
  else
    for FormNr = 0 to IE.Document.Forms.Length - 1
      Set TheForm = IE.Document.Forms(FormNr)
      for ItemNr = 0 to TheForm.Elements.Length - 1
        if TheForm.Elements(ItemNr).Name = fieldname then
          TheForm.Elements(ItemNr).Value = fieldvalue
          If submit=1 then
            TheForm.submit
          end if
          exit for
        end if
      next
    next
  end if
End Sub

'Navigates IE to specified URL
Sub Navigate(URL)
  IE.Navigate URL
  do while IE.Busy
  loop
End Sub

'clicks specified link
Sub ClickLink(linktext)
  Dim anchors
  Dim ItemNr

  Set anchors = IE.document.getElementsbyTagname("a")
  For ItemNr = 0 to anchors.length - 1
    If anchors.Item(ItemNr).innertext = linktext Then
	  anchors.Item(ItemNr).click
	End If
  next

  do while IE.Busy
  loop
End Sub

'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
  dim t
  set t = IE.document.getElementsbyTagname(Tagname)
  if all=1 then
    ExtractTag = t.Item(Num).outerHTML
  else
    ExtractTag = t.Item(Num).innerText
  end if
End Function

VBEND

//Find existing IE window
VBRun>GetIE,www.google.com

//enter search query and submit
VBRun>WebFormFill,q,windows macro,1

//Click a link
VBRun>ClickLink,Advanced Search

//extract 
VBEval>ExtractTag("td",5,0),tagText
MessageModal>tagText
The implementation of GetIE shown here accepts a string. GetIE then looks for a copy of IE whose location URL contains that string. So it allows you to find a copy of IE based on the URL it is currently at.

This example attaches to a copy of IE currently open at http://www.google.com. So to run this example first open IE and go to http://www.google.com. Then run the script and it will enter a search term, click the Advanced Search link and then extract some text from a TD tag.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Mar 15, 2014 2:22 pm

Please note that the information given above has been superseded by the native Macro Scheduler IE functions, one of which is now IEGetFromURL which will get a reference to an IE window via its URL.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts