There are various scripts on the forums demonstrating how to automate Internet Explorer via VBScript and IE’s Document Object Model. Such as this one: Automate web forms with IE
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:
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 www.google.com. So to run this example first open IE and go to 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.