IE Automation - vba-VBForums
Results 1 to 11 of 11

Thread: IE Automation - vba

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    4

    IE Automation - vba

    Hello VB Forums,

    I have been trying to get some help regarding this topic from other forums they have proven to be very unhelpful.
    I shouldnt think this will be too difficult for any experienced programmer so I hope I am able to find some help on this forum.

    There are a couple of things I need to understand for automating IE using a Macro/VBScript.
    I am a semi-beginner user of vba codes and I only got as far as the codes below as an exercise for IE automations.
    Essentially, I would like to do the following:

    1. click on a hyperlink on 1st IE window to open 2nd window
    2. click on a couple of buttons on the second IE window, close 2nd IE window and move back to the 1st IE window.

    (I understand this involves knowing the "handle" / Names of the windows and being able to switch from each other?)

    I would be grateful if anyone can assist me on this, many thanks.

    regards,
    Ed

    Please understand that the below is only an exercise

    Code:
    Sub Search_Google()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    
    IE.Navigate "http://www.google.com" 'load web page google.com
    
    While IE.Busy
      DoEvents  'wait until IE is done loading page.
    Wend
    
    IE.Document.all("q").Value = "what you want to put in text box"
    ie.Document.all("btnG").Click 
    'clicks the button named "btng" which is google's "google search" button
    
    While ie.Busy
      DoEvents  'wait until IE is done loading page.
    Wend
    
    End Sub

  2. #2
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: IE Automation - vba

    You can open a new window through the DOM by setting the link's target to a _blank or such.
    Code:
    IE.document.links(0).target = "_blank"
    IE.document.links(0).Click
    This isn't a great way since you will then (unless I am mistaken) need to use API to find the new window.

    I would suggest possibly creating two IE objects.
    Loading the first as you have. Then instead of clicking the link simply grab the Href and load it into the second IE object.


    Code:
    Dim IE As Object
    Dim IE2 As Object
    
    Sub Search_Google()
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    
    IE.Navigate "http://www.google.com" 'load web page google.com
    
    While IE.Busy
      DoEvents  'wait until IE is done loading page.
    Wend
    
    Dim LinkHref As String
    
    LinkHref = IE.document.links(0).href
    
    
    Set IE2 = CreateObject("InternetExplorer.Application")
    IE2.Visible = True
    
    IE2.Navigate LinkHref
    
    
    
    While IE.Busy
      DoEvents  'wait until IE is done loading page.
    Wend
    
    
    End Sub
    
    Private Sub Command1_Click()
    
    Search_Google
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    Set IE = Nothing
    If TypeName(IE) <> "Nothing" Then Unload IE
    Set IE2 = Nothing
    If TypeName(IE2) <> "Nothing" Then Unload IE2
    
    End Sub

    Since you now have another IE object to reference the new window it is no different than simply doing a IE2.document.links(INDEX).click or whatever you want.

    I don't doubt there is probably a better way,
    Mike

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: IE Automation - vba

    Moved to Office Development
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    4

    Re: IE Automation - vba

    Thanks for the prompt response MikeJoel, I will take a look and get back to you
    Regards,
    Ed

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    4

    Re: IE Automation - vba

    Your codes are very help, thanks.
    Though any chance you can give me an example for selecting javascripts on the google map webpage, the send button on the right above the map.
    Many thanks
    Regards,
    Ed

    http://maps.google.co.uk/maps?hl=en&tab=wl

  6. #6
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: IE Automation - vba

    Quote Originally Posted by papapa
    Your codes are very help, thanks.
    Though any chance you can give me an example for selecting javascripts on the google map webpage, the send button on the right above the map.
    Many thanks
    Regards,
    Ed

    http://maps.google.co.uk/maps?hl=en&tab=wl
    I don't really understand what you mean by "selecting javascipts".
    To click the search maps button use,
    IE.Document.All("q_sub").Click
    To click the button.

    To find the Names and IDs of the elements just look at the page's source code.

    Mike

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    4

    Re: IE Automation - vba

    Many thanks MikeJoel, I will take a look and get back to you =)
    Regards,
    Ed

  8. #8
    New Member
    Join Date
    Jun 2009
    Posts
    4

    Re: IE Automation - vba

    Hi

    Is it possible to access contents of a page that comes after login page

    means i have automated login to a site by putting usename and password
    and clicked on submit

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Navigate2 URL

    Do Until IE.ReadyState = READYSTATE_COMPLETE
    Loop

    IE.Visible = True

    IE.Document.all("userName").Value = Worksheets("Sheet1").userid.Text
    IE.Document.all("passWord").Value = Worksheets("Sheet1").pwd.Text
    IE.Document.all("Submit").Click


    now i want a click on the link from the first page that comes after login
    IE.Document.Links(0).target = "_blank"
    IE.Document.Links(0).Click

    but I can not use the same IE object to access link, text box or buttons from next page

    at the begining of this thread some ' handle ' is mentioned how to use this concept ?
    Please guide ...
    Thanks in advance

  9. #9
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Question Re: IE Automation - vba

    Thats a wonderfull explanation.

    I have a problem with mine, instead of it being a submit button, mine is a link with a javascript:

    javascript: document.authform.submit();

    How do I get this to work?

  10. #10
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: IE Automation - vba

    Sorry yea it works now didnt see the bit with the link, must be having a bad day today in retrospect lol, wonderfull!

    How do I select Radio buttons and select or drop down boxes?

    Thats really what I want out of this.

    Then to be able to say does a certain text exist in web page, then logout and close internet explorer, is this possible?

    Thats wonderfull so far, saved me a whole lot of programming this has.

  11. #11
    New Member
    Join Date
    Oct 2011
    Posts
    1

    Exclamation Re: IE Automation - vba

    Important:
    Set IE2 = Nothing
    is not enought for clean up!
    Don't forget
    EI2.Quit
    Otherwise you run in trouble (memory, processes, the computer become extremely slow, ...)
    I had to learn it the hard way, when extending ie automation in an excel-sheet we use for daily statistic and share it via the syncing.net software in our company. More than 30 pc got it, all pc became terrible slow, ...
    Let's formulate it positive, colleagues were happy after the quit line was inserted and the count of iexplorer processes was not increasing anymore (every instance consuming 100 MB of memory).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.