{"id":247,"date":"2008-05-07T09:57:21","date_gmt":"2008-05-07T09:57:21","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=247"},"modified":"2008-05-07T09:57:21","modified_gmt":"2008-05-07T09:57:21","slug":"attaching-to-an-existing-internet-explorer-instance","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2008\/05\/07\/attaching-to-an-existing-internet-explorer-instance\/","title":{"rendered":"Attaching to an existing Internet Explorer instance"},"content":{"rendered":"<p>There are various scripts on <a href=\"http:\/\/www.mjtnet.com\/usergroup\/\">the forums<\/a> demonstrating how to automate Internet Explorer via VBScript and IE&#8217;s Document Object Model. Such as this one: <a href=\"http:\/\/www.mjtnet.com\/usergroup\/viewtopic.php?t=1461\">Automate web forms with IE<\/a><\/p>\n<p>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.<\/p>\n<p>A question that has come up a few times is &#8220;How do I connect to an already running copy of IE, one that the script didn&#8217;t start?&#8221;. This is what the GetObject function is designed for, but Internet Explorer doesn&#8217;t support it.<\/p>\n<p>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:<\/p>\n<pre name=\"code\" class=\"macroscript\">VBSTART\n\nDim IE\n\n'Attaches to an existing instance of IE with matching URL\nSub GetIE(URL)\n  Dim objInstances, objIE\n  Set objInstances = CreateObject(\"Shell.Application\").windows\n  If objInstances.Count > 0 Then '\/\/\/ make sure we have instances open.\n    For Each objIE In objInstances\n\t  If InStr(objIE.LocationURL,URL) > 0 then\n\t    Set IE = objIE\n\t  End if\n    Next\n  End if\nEnd Sub\n\n'fills a form field and optionally submits form\nSub WebFormFill(fieldname,fieldvalue,submit)\n  Dim FormNr\n  Dim ItemNr\n  Dim TheForm\n\n  if IE.Document.All.Tags(\"FORM\").Length = 0 then\n    MsgBox(\"No form found in page\")\n  else\n    for FormNr = 0 to IE.Document.Forms.Length - 1\n      Set TheForm = IE.Document.Forms(FormNr)\n      for ItemNr = 0 to TheForm.Elements.Length - 1\n        if TheForm.Elements(ItemNr).Name = fieldname then\n          TheForm.Elements(ItemNr).Value = fieldvalue\n          If submit=1 then\n            TheForm.submit\n          end if\n          exit for\n        end if\n      next\n    next\n  end if\nEnd Sub\n\n'Navigates IE to specified URL\nSub Navigate(URL)\n  IE.Navigate URL\n  do while IE.Busy\n  loop\nEnd Sub\n\n'clicks specified link\nSub ClickLink(linktext)\n  Dim anchors\n  Dim ItemNr\n\n  Set anchors = IE.document.getElementsbyTagname(\"a\")\n  For ItemNr = 0 to anchors.length - 1\n    If anchors.Item(ItemNr).innertext = linktext Then\n\t  anchors.Item(ItemNr).click\n\tEnd If\n  next\n\n  do while IE.Busy\n  loop\nEnd Sub\n\n'This function extracts text from a specific tag by name and index\n'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)\n'set all to 1 to extract all HTML, 0 for only inside text without HTML\nFunction ExtractTag(TagName,Num,all)\n  dim t\n  set t = IE.document.getElementsbyTagname(Tagname)\n  if all=1 then\n    ExtractTag = t.Item(Num).outerHTML\n  else\n    ExtractTag = t.Item(Num).innerText\n  end if\nEnd Function\n\nVBEND\n\n\/\/Find existing IE window\nVBRun>GetIE,www.google.com\n\n\/\/enter search query and submit\nVBRun>WebFormFill,q,windows macro,1\n\n\/\/Click a link\nVBRun>ClickLink,Advanced Search\n\n\/\/extract\nVBEval>ExtractTag(\"td\",5,0),tagText\nMessageModal>tagText<\/pre>\n<p>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.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are various scripts on the forums demonstrating how to automate Internet Explorer via VBScript and IE&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/247"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":0,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}