none
How to Control a Second IE Browser? RRS feed

  • Question

  • Dear Excel Experts-

     

    I am trying to use the code below to automate the process of entering data into a web page, running a validation process, and extracting information from another web page.  Below is my code:

     

    Option Explicit

     

    Sub Derivatives()

     

    Dim oHTML_Element As IHTMLElement

    Dim sURL As String

    Dim HTMLDoc As HTMLDocument

    Dim oBrowser As InternetExplorer

    Dim ie As Variant

    Dim ie2 As Variant

    Dim itm As Variant

    Dim td As HTMLInputElement

    Dim a As Variant

    Dim arr As Variant

    Dim textarea As Variant

    Dim Mystr As String

       

        Const DELIMITER = vbCr

        Const NEW_URL = "https://moap.it . . . ProfitCenter"

        Set ie = CreateObject("InternetExplorer.Application")

       

        ie.Visible = True

        ie.navigate "https://moap.it.cib. . . .type=standard"

        'While ie.Busy: DoEvents: Wend

     

        Do While ie.Busy = True Or ie.readyState <> 4

             DoEvents

        Loop

        

    While ie.Busy: DoEvents: Wend

    Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)

    If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.db.moap.report.FOBOJournalReport"

     

    For Each oHTML_Element In ie.document.getElementsByTagName("input")

        If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For

    Next

     

    'While ie.Busy: DoEvents: Wend

    Do While ie.Busy = True Or ie.readyState <> 4

         DoEvents

    Loop

    Set itm = ie.document.getElementsByName("report.field[0].value")(0)

    If Not itm Is Nothing Then itm.Value = "31-12-2010"

     

    Set itm = ie.document.getElementsByName("report.field[4].Value")(0)

    If Not itm Is Nothing Then itm.Value = "DBLN"

     

    Set itm = ie.document.getElementsByName("report.field[5].value")(0)

    If Not itm Is Nothing Then itm.Value = "DERIVATIVES"

     

    Set itm = ie.document.getElementsByName("report.field[1].valueSelect")(0)

    If Not itm Is Nothing Then itm.Value = "All"

     

    'Working . . .

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a")(0)

                If Not a Is Nothing Then

                    ie.navigate a.href

                    Mystr = a.href

                    MsgBox (Mystr): Exit For

                End If

            End If

        End If

    Next

     

     

    'WORKING

      Set ie2 = CreateObject("InternetExplorer.Application")

       ie2.Visible = True

       While ie2.readyState <> 4: DoEvents: Wend

       ie2.navigate Mystr

     

     

       Set textarea = ie2.document.getElementById("pasteProfitCenter")

       If Not textarea Is Nothing Then

     

          Sheets("Sheet1").Select

          With ActiveSheet 'Sheets("PC Listing")

             arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

          End With

     

          If Not IsArray(arr) Then arr = Array(arr)

          textarea.Value = Join(arr, DELIMITER)

       End If

     

    For Each oHTML_Element In ie2.document.getElementsByTagName("input")

           If oHTML_Element.Value Like "*Validate-->*" Then oHTML_Element.Click:

           Debug.Print oHTML_Element.Value

    Next

     

    ‘couple formatting things

     

    End Sub

     

    The code stops on this line:

    Set ie2 = CreateObject("InternetExplorer.Application")

     

    I get this message popping up:

    Javascript:openwindowlinkAdvanced()

     

    The scenarios is like this:

    I want to navigate to a page, click an href named "(advanced profit centre selection)", copy data from my Excel sheet, and paste it into a TextArea element named "pasteProfitCenter", click a button named "Validate" and then click a button named "Continue".  This almost works, but excel seems to loose its focus, or something like that, when it clicks on the advanced profit center selection, which is this:

    Mystr = a.href

     

    After the href is clicked, a second IE browser opens, but I can’t seem to reference it:

    Set ie2 = CreateObject("InternetExplorer.Application")

     

    That doesn’t work!  Then, I lose control of everything!!

     

    Does anyone have any ideas of how to control a second instance of IE?

     

    Thanks!!

    Ryan--

     

     

    Saturday, January 8, 2011 1:19 AM

All replies

  • Ryan : Here we go again.  I should just come into the city and solver the problem personally.  The answer is very simple.  The second ie explorer has no URL.

     

    From

    'Working . . .

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a")(0)

                If Not a Is Nothing Then

                    ie.navigate a.href

                    Mystr = a.href

                    MsgBox (Mystr): Exit For

                End If

            End If

        End If

    Next

     

     

    'WORKING

      Set ie2 = CreateObject("InternetExplorer.Application")

       ie2.Visible = True

       While ie2.readyState <> 4: DoEvents: Wend

       ie2.navigate Mystr

     

    To :

    'Working . . .

    set a = nothing

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

               Set a = td.getElementsByTagName("a")(0)

               Exit for 

            End If

        End If

    Next

     

     

    'WORKING

     If Not a Is Nothing Then

        Set ie2 = CreateObject("InternetExplorer.Application")

        ie2.Visible = True

     

        

    Mystr = a.href

     

        MsgBox (Mystr)

        ie2.navigate a.href

       

    While ie2.readyState <> 4: DoEvents: Wend

     

    end if

     
    jdweng
    Saturday, January 8, 2011 12:48 PM
  • After a nice relaxing weekend doing nothing but reading ‘Black Swan’ and ‘The Quants’, I’m back in the office now.  I just tried your code, Joel.  It looks fine to me, but now Excel is throwing a fit.  I get an error here:

    While ie2.readyState <> 4

     

    The error message reads: ‘Run-time error. Automation error, The object invoked has disconnected from its clients.’

     

    Then I get an error message box:  ‘Internet Explorer cannot download. Unspecified Error.’

     

    That’s basically what I had before.  Is it not???  However, now ie2 doesn’t even open.  Before it opened, but I couldn’t reference it.  Now, I encounter an error before it even opens, so there is no way I can reference it.

     

    To me, it seems like this ie2 is some kind of ‘special window’; the browser is much smaller than a normal browser. 

     

    I experimented with the code a bit; I just changed it to this:

     

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moar-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a")(0)

                    Mystr = a.href

                    Exit For

            End If

        End If

    Next

     

    If Not a Is Nothing Then

    Set ie2 = CreateObject("InternetExplorer.Application")

       ie2.Visible = True

       While ie2.readyState <> 4: DoEvents: Wend

       ie2.navigate Mystr

     

    Now, it get stuck here:

    While ie2.readyState <> 4

     

    There is no error, but it goes into a perpetual loop.

     

    I would love to get this working, but even if I have to click through the menus the old fashioned way, I would say that I will come away from this experience much wiser, and that ‘education’ will transfer, with me, to the next project.

     

    Any other ideas??? 

    Monday, January 10, 2011 2:20 PM
  • What is the webpage doing?  Make sure the webpage has the correct URL in the address box.

     

    I would also check for IE busy like the code below for both web browsers

    IE.Navigate2 URL
    Do While IE.readyState <> 4 Or _
       IE.Busy = True

       DoEvents
    Loop


    jdweng
    Monday, January 10, 2011 2:47 PM
  • I ran through the logic again; this works fine:

    For Each td In ie.document.getElementsByTagName("td")

          If td.className = "moar-td-form-obj" Then

          If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a").Item(0)

            If Not a Is Nothing Then

                ie.navigate a.href

                Exit For

            End If

          End If

          End If

    Next

     

    Set ie2 = CreateObject("InternetExplorer.Application")

     

    Now my second ie2 (it’s very small) opens up and if I manually copy/paste the array from Excel to the TextArea named "pasteProfitCenter", everything is fine.  Without manual intervention nothing seems to work.  Excel just won’t recognize the second instance of IExplorer.  For instance, when I step through to the lines below, I get another error:

     

    Set textarea = ie2.document.getElementById("pasteProfitCenter")

    If Not textarea Is Nothing Then

    Sheets("Sheet1").Select

    With ActiveSheet

          arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

    End With

     

    If Not IsArray(arr) Then arr = Array(arr)

          textarea.Value = Join(arr, DELIMITER)

    End If

     

    I immediately get an error on this line:

    Set textarea = ie2.document.getElementById("pasteProfitCenter")

     

    ‘Run-time error. Method document of ‘IWebBrowser2’ failed’.

     

     

    Joel, do you know of any written documentation, perhaps web-based, that describes how this stuff works?  I saw something at MASD before; it was very terse and completely feeble, so I didn't save the link under 'Favorites'.

    Monday, January 10, 2011 3:47 PM
  • Ryan : I dodn't see the 2nd IE navigating to a webpage.

    My fix had the code below.  I did the following

    1. Created 2nd IE
    2. Navigated to URL
    3. Waited for webpage to become ready.

    'WORKING

     If Not a Is Nothing Then

        Set ie2 = CreateObject("InternetExplorer.Application")

        ie2.Visible = True

     

        

    Mystr = a.href

     

        MsgBox (Mystr)

        ie2.navigate a.href

       

    While ie2.readyState <> 4: DoEvents: Wend


    jdweng
    Monday, January 10, 2011 5:20 PM
  • Let me try to distill this into the most essential parts here.

    Everything works fine up to here:

    ie.navigate a.href

     

    That line will open the second (small) ie2 browser.  This is great!!  If I copy/paste data from the sheet into the TextBox with td class ‘Copy&paste profit centre list :’, the results are displayed in the TextBox with td class ‘Final profit centres list :’!  This is what I want, but it is manual.  When the code runs, it doesn’t really do what I described.

     

    After the ie2 opens, with this:

    ie.navigate a.href

     

    Then, this line:

    Exit For

     

    Takes me to these two lines:

    If Not a Is Nothing Then

    Set ie2 = CreateObject("InternetExplorer.Application")

     

    But, this line doesn’t seem right:

    Mystr = a.href

     

    The variable Mystr = javascript:openwindowlinkAdvanced()

     

    Then, this next line causes an error:

    ie2.navigate a.href

     

    Error message is:

    ‘Internet Explorer cannot download. Unspecified error.’

     

    I’ve tried many things; some posted here many are not.  Excel just can’t seem to recognize that ie2. 

     

    I’m using this:

    Dim ie2 As Variant

     

    I’m sure the variable is there (and of the right type), but Excel doesn’t seem to be able to access it.

     

    I’m going to try a couple more ideas now.  If you can think of a workaround, please do post back.

     

    As always, thanks so much!!

    Ryan---

     

    Tuesday, January 11, 2011 12:48 AM
  • Ryan : the 1st webpage has changed pages in this code

    After the ie2 opens, with this:

    ie.navigate a.href

    The fix I sent you removed the navigate line.  If you move the 1st webpage then you loose the "a" object and this line won't work

    Mystr = a.href


    jdweng
    Tuesday, January 11, 2011 10:38 AM
  • Joel, I tried your last recommendation; it didn't work.

     

    I believe this can be done, and it may not even be hard, but I’m not exactly sure how to explain what I’m seeing . . .  Anyway, let me just try to explain this as best as I can.

     

    This part works fine:

    For Each td In ie.document.getElementsByTagName("td")

          If td.className = "moap-td-form-obj" Then

          If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a").Item(0)

            If Not a Is Nothing Then

                ie.navigate a.href

                Exit For

            End If

          End If

          End If

    Next

     

    This line pops open the smaller IE window:

    ie.navigate a.href

     

    If I right-click on ie, in the WtachWindow, I see that the LocationURL is correct.

     

    The second ie browser is open at this point!!!  I think that is critical to solving this problem.  I don’t have to fire the below-line of code to see the second ie window:

    ie2.Visible = True

     

    First Try:

    So, if I continue on, stepping through the code . . .

    If Not a Is Nothing Then

    Set ie2 = CreateObject("InternetExplorer.Application")

    ie2.navigate NEW_URL

    ie2.Visible = True

       While ie2.readyState <> 4: DoEvents: Wend

    End If

     

       Set textarea = ie2.document.getElementById("invalidProfitCenter")

       If Not textarea Is Nothing Then

     

        Sheets("Sheet1").Select

        With ActiveSheet

            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

            If Not IsArray(arr) Then arr = Array(arr)

            Set itm = ie2.document.getElementById("pasteProfitCenter")

            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)

        End With

        End If

     

    At this point, I have TWO ie browers open.

    Now, that copies/pastes an array from my Excel sheet into a TextArea in the second web browser.  So that part it fine, but then I (manually) click a button named ‘Validate’ and the whole process freezes.  I see a small exclamation mark, with a yellow triangle behind it, in the lower left hand corner of the second browser window.

     

    This error reads:

    Webpage error details

     

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; .NET CLR 1.1. . . etc . . .)

    Timestamp: Wed, 12 Jan 2011 23:33:20 UTC

     

    Message: 'window.opener.document' is null or not an object

    Line: 106

    Char: 2

    Code: 0

    URI: https://moap.it. . . etc . . . ProfitCenter

     

    That’s the problem, in a nutshell.  Does it make sense?  I’ve never dealt such a thing before.  I googled for an answer, but didn't find any useful documentation about this anywhere, so I don’t know how to overcome this issue.

     

    Second Try:

    I changed the code a bit; similar to what you recommended before:

    For Each td In ie.document.getElementsByTagName("td")

          If td.className = "moap-td-form-obj" Then

          If td.innerText Like "*(advanced profit centre selection)*" Then

                Set a = td.getElementsByTagName("a")(0)

                Exit For

            End If

          End If

    Next

     

    If Not a Is Nothing Then

    Set ie2 = CreateObject("InternetExplorer.Application")

    ie2.navigate NEW_URL

    ie2.Visible = True ‘< -- This line pops open the second ie browser; it does NOT open before this!!

       While ie2.readyState <> 4: DoEvents: Wend

    End If

     

       Set textarea = ie2.document.getElementById("invalidProfitCenter")

       If Not textarea Is Nothing Then

     

        Sheets("Sheet1").Select

        With ActiveSheet

            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

            If Not IsArray(arr) Then arr = Array(arr)

            Set itm = ie2.document.getElementById("pasteProfitCenter")

            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)

        End With

        End If

     

    This will copy/paste n array from my Excel sheet into a TextArea in the second web browser, just like I described above, and I get the same error, just like I described above.

     

    The difference is, during the Second Try, I have THREE ie browsers open; the initial one, and somehow two ie2 browsers.  I’m a bit lost now.  No matter what I do, I can’t seem to control the ie browser that opens with this line of code:

    ie.navigate a.href

     

    or, this line of code:

    ie2.Visible = True

     

    Do you have any thoughts on this, Joel?

     

    As always, thanks!!

    Wednesday, January 12, 2011 11:55 PM
  • Look at these lines of code

     

    If Not a Is Nothing Then

                ie.navigate a.href

                Exit For

    1. You haven't created a 2nd IE application yet but another window opens.
    2. You navigated away from the URL where the variables "a" is set.  The variable "a" is now point to nothing.  The variable "a" is an object on the initial webpage and you lost it by navigating to another URL.
    3. You don't want to navigate the first webpage to another location, instead you need create the 2nd IE and then have the 2nd IE naviagate to the URL a.href.


    jdweng
    Thursday, January 13, 2011 12:54 AM
  • That certainly sounds right!  The code is still not right thought.  Just tried this:

    If Not a Is Nothing Then

    Set ie2 = CreateObject("InternetExplorer.Application")

    ie2.navigate a.href

       While ie2.readyState <> 4: DoEvents: Wend

    End If

     

    Error message is: ‘Internet explorer cannon download. Unspecified error.’

    ‘Automation error. The object invoked has disconnected from it’s client.’

     

    Looking for ie2 in the WatchWindow; Value = empty and Type = Empty – that’s not it.

     

    Tried this too:

    If Not a Is Nothing Then

    ie2.navigate a.href

       While ie2.readyState <> 4: DoEvents: Wend

    End If

     

    Error message: ‘object required’ . . . this makes sense since it wasn’t set (I thought, maybe, somehow, the ie2 object would be inherited from the ie object).

     

    What am I doing wrong now?

     

    Thursday, January 13, 2011 1:37 AM
  • I'm only seeing pieces of the code so I can find the error.  You need to make sure a.href really points to a URL.  add the object "a" to the watch window and step through the code.  Look at a.ref in the watch to find the source of the problem.  Make sure you know which IE explorer is being excercised as you go through the code.  You could have 3 different IE explorers since the navigating to a.href seems to open a new explorer.
    jdweng
    Thursday, January 13, 2011 10:30 AM
  • I’m using the WatchWindow for every step now; still stuck.  Here’s all the code:

     

    Sub Derivatives()

    Dim oHTML_Element As IHTMLElement

    Dim sURL As String

    Dim HTMLDoc As HTMLDocument

    Dim oBrowser As InternetExplorer

    Dim ie As Variant

    Dim ie2 As Variant

    Dim itm As Variant

    Dim td As HTMLInputElement

    Dim a As Variant

    Dim arr As Variant

    Dim textarea As Variant

    Dim Mystr As String

       

    Const DELIMITER = vbCr

    Const NEW_URL = "https://moap.it . . . ProfitCenter"

    Set ie = CreateObject("InternetExplorer.Application")

     

    ie.Visible = True

    ie.navigate "https://moap.it.cib. . . .type=standard"

    'While ie.Busy: DoEvents: Wend

     

    Do While ie.Busy = True Or ie.readyState <> 4

    DoEvents

    Loop

     

    While ie.Busy: DoEvents: Wend

    Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)

    If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.JournalReport"

     

    For Each oHTML_Element In ie.document.getElementsByTagName("input")

        If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For

    Next

     

    . . . here I’m using lots of code to enter data from the Excel sheet into the browser; all of this works fine . . .

     

    For Each td In ie.document.getElementsByTagName("td")

          If td.className = "moap-td-form-obj" Then

          If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a").Item(0)

            If Not a Is Nothing Then

                'ie.navigate a.href . . . commented out now . . . see notes below

                a.href = NEW_URL

                Exit For

            End If

          End If

          End If

    Next

     

    If Not a Is Nothing Then

        Set ie2 = CreateObject("InternetExplorer.Application")

        ie2.Visible = True

        Mystr = a.href

        ie2.navigate a.href

    While ie2.readyState <> 4: DoEvents: Wend

    End If

     

       Set textarea = ie2.document.getElementById("invalidProfitCenter")

       If Not textarea Is Nothing Then

     

        Sheets("Sheet1").Select

        With ActiveSheet

            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

            If Not IsArray(arr) Then arr = Array(arr)

            Set itm = ie2.document.getElementById("pasteProfitCenter")

            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)

        End With

        End If

     

    End Sub

     

    I commented out this piece:

    ie.navigate

    In the WatchWindow I’m getting this message:

    Watch :   : ie.navigate : <Wrong number of arguments or invalid property assignment> : Variant/Integer : Module2.Derivatives

     

    Also, commented this out:

    a.href

    In the WatchWindow I’m getting this message:

    Watch :   : a.href : "javascript:openwindowlinkAdvanced()" : Variant/String : Module2.Derivatives

     

    Instead, I’m using this:

    a.href = NEW_URL

    NEW_URL is assigned at the beginning of the code; this is the URL from the second browser that opens. This is what I’m trying to control!!

    In the WatchWindow, I can now see that a.href is this:

    Watch :   : a.href : "https://moap.it . . . ProfitCenter": Variant/String : Module2.Derivatives

     

    In WatchWindow ie2.navigate gives me this:

    Watch :   : ie2.navigate : <Wrong number of arguments or invalid property assignment> : Variant/Integer : Module2.Derivatives

     

    That’s weird!  Anyway, I have an array of cost center IDs that I copy from the Excel sheet and paste into the second browser (goes into an object called textarea).  The data is copied from Excel and paster into the textarea just fine.  When I manually click the 'Validate' button, nothing happens (the browser seems to freeze up).  Also, I still see the small exclamation mark, with a yellow triangle behind it, in the lower left hand corner of the second browser window.

     

    What do you think of this?  I’ve already spent some considerable time on this.  It would be a huge win for me to get this working!!

     

    Ryan--

    Thursday, January 13, 2011 2:12 PM
  • Remove this statement for the 1st IE

    a.href = NEW_URL

    You are over writing the value of a.href and putt 0 into a.ref.  N ew_URL is empty!!!


    jdweng
    Thursday, January 13, 2011 5:05 PM
  • No, the URL is a link to a secure site:

    Const NEW_URL = "https://moap.it . . . ProfitCenter"

    That's the second browser window that pops open...

     

    Made a slight change in the order of things:

    For Each td In ie.document.getElementsByTagName("td")
          If td.className = "moap-td-form-obj" Then
          If td.innerText Like "*(advanced profit centre selection)*" Then
            Set a = td.getElementsByTagName("a").Item(0)
            If Not a Is Nothing Then
                Set ie2 = CreateObject("InternetExplorer.Application")
                a.href = NEW_URL
                ie2.navigate a.href
                Exit For
            End If
          End If
          End If
    Next

    If Not a Is Nothing Then
        Set ie2 = CreateObject("InternetExplorer.Application")
        ie2.Visible = True
        ie2.navigate a.href
    While ie2.readyState <> 4: DoEvents: Wend
    End If


       Set textarea = ie2.document.getElementById("invalidProfitCenter")
       If Not textarea Is Nothing Then

        Sheets("Sheet1").Select
        With ActiveSheet
            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))
            If Not IsArray(arr) Then arr = Array(arr)
            Set itm = ie2.document.getElementById("pasteProfitCenter")
            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)
        End With
        End If

    I get essentially the same results:

    Small yellow triangle and exclamation mark! 

    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
    Timestamp: Fri, 14 Jan 2011 02:15:25 UTC


    Message: 'window.opener.document' is null or not an object
    Line: 106
    Char: 2
    Code: 0
    URI:

    It's counterintuitive that Excel knows how to pasts the data to the page, but then it immediately forgets what it's doing and causes the page to disconnect from the server (I guess...based on this message...'window.opener.document' is null or not an object).

     

     

    Thursday, January 13, 2011 11:50 PM
  • This may be the key:
    If td.innerText Like "*(advanced profit centre selection)*" Then
    This is a hyperlink:  (advanced profit centre selection)
    How can I click that using VBA?

    I thought it was:
    ie.navigate a.href

    However, that's giving me an error: 'Run-time 424: Object required'
    That doesn't make sense because I dimmed my first ie and controlled it just fine, as exemplified below:

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate "https://moap.it . . . . standard"

    Set itm = ie.document.getElementsByName("report.field[5].value")(0)
    If Not itm Is Nothing Then itm.Value = "DERIVATIVES"

    Set itm = ie.document.getElementsByName("report.field[1].valueSelect")(0)
    If Not itm Is Nothing Then itm.Value = "All"

    Everything in the first browser works just fine.  I think I am just not launching the second browser properly, and that’s why I’m experiencing erratic behavior.

    Friday, January 14, 2011 2:02 PM
  • I think if I click the right object, I can launch the second browser, and control it thr right way. 

    How would I click one of these objects listed below?


    <td class="moap-td-form-label">

    Text - Profit Centre Advanced :

    <td class="moap-td-form-obj">

    <script language="javascript">function openwindowlinkAdvanced() {       newwin = window.open("","windownameAdvanced","toolbar=yes,location=yes,height=550,width=680,left=300,top=100,screenX=500,screenY=100,scrollbars=yes,resizable");</script>

    <!--  input type="hidden" name="profitCenterAdvanced" id="profitCenterAdvanced" value="" -->

    <a href="javascript:openwindowlinkAdvanced()">

    Text - (advanced profit centre selection)

    Text - Empty Text Node

    Thanks Joel!


    I don't think these lines are necessary:
    Set ie2 = CreateObject("InternetExplorer.Application")
    ie.navigate a.href


    Those commands are definitely causing some problems for me. 

    Friday, January 14, 2011 11:46 PM
  • If you have a button I use CLICK method or when I have a form a use submit.

     

    Something like this

     

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

     

           td.submit

       End If

    Next


    jdweng
    Saturday, January 15, 2011 12:31 PM
  • Thanks again Joel.  Just tried it; got this message:

    Run-time error '438'.  Object doesn't support this property or method.

    The error is on this line:

    td.submit

    I tried to find out what that is; in the Watch Window I get the same thing:

    <Object doesn't support this property or method>

    I have a quick question.  How can I control the 'a' below?

    For Each td In ie.document.getElementsByTagName("td")
        If td.className = "moar-td-form-obj" Then
            If td.innerText Like "*(advanced profit centre selection)*" Then
            Set a = td.getElementsByTagName("a")(0)
                    a.Click
                    Exit For
            End If
        End If
    Next

    In the Watch Window, 'a.Click' has a Value of 'Empty', but in the Watch Window, 'a' is this:

    Value = "javascript:openwindowlinkAdvanced()"

    href = "javascript:openwindowlinkAdvanced()"

    ie8_href = "javascript:openwindowlinkAdvanced()"

    nameProp = "javascript:openwindowlinkAdvanced()"

    outerHTML = "<A href="javascript:openwindowlinkAdvanced()">(advanced profit centre selection)</A>"

    that doesn't seem like it would have any properties of an ie browser.  That may be the whole problem here!!!

    I tried this:

    a.Click
    a.navigate NEW_URL

    Then, looked at 'a.navigate' in the Watch Window, and got this (just like the issue described above):

    Run-time error '438'.  Object doesn't support this property or method.

    So, again, the 'a.Click' open a new browser window, but that object doesn't seem to have any properties of a browser.  I think that's the whole problem here.  How can I resolve this?

    I'm going to try some more ideas now.  I'll get this working eventually.  Really, at this point, it's going to be just pure luck, when I find the magic line of code that makes this work.  If you have any more ideas, please post back, Joel!!!

    Sunday, January 16, 2011 6:23 PM
  • I rearranged the code a bit:

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a")(0)

                    'a.Click

                    Set ie2 = CreateObject("InternetExplorer.Application")

                        Sheets("Sheet1").Select

                        With ActiveSheet

                            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

                            If Not IsArray(arr) Then arr = Array(arr)

                            Set itm = ie2.document.getElementById("pasteProfitCenter")

                            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)

                        End With

                    Exit For

            End If

        End If

    Next

     

    Notice:  'a.Click

    With that line commented out, the second browser opens just fine (I didn’t expect that).

    Anyway, the line below causes an error:

    Set itm = ie2.document.getElementById("pasteProfitCenter")

     

    Error message is: ‘Run-time error 2147467259’

    Method ‘Document’ of object ‘IWebBrowser2’ failed

     

    What does this mean?  I think I’m close to a solution now!  I’ve seen some documentation online about

    IWebBrowser2

     

    I haven’t really seen any solutions thought.

    Sunday, January 16, 2011 8:02 PM
  • I would first try to see if there are any forms on the page.

     

    set Form = td.getElementsByTagName("Form")

    Then

    Form.submit  or  Form(0).submit

    You may have more than one form.  Adding a watch will tell you for sure.

     

    Your code should be this

    From :

    Set a = td.getElementsByTagName("a")(0)
                    a.Click

    To :

    Set a = td.getElementsByTagName("a")
                    a(0).Click

    There are probably a lot of objects on the page that have tags "a".  Only objects with the property Click or there children will the Click or form Method work.

     

     


    jdweng
    Sunday, January 16, 2011 8:09 PM
  • The 2nd Browser you didn't naviagate any place.  The window is at the default HOMEPAGE.  Apparently your home page has the property pasteProfitCenter.
    jdweng
    Sunday, January 16, 2011 8:15 PM
  • Thanks again, Joel.  I switched the (0) around; didn’t make a different.  I used to know the reason for the (0), but forgot now.  Zero-based index??

     

    I got a sample of a function from a scripting discussion forum; implemented below:

    For Each td In ie.document.getElementsByTagName("td")

        If td.className = "moap-td-form-obj" Then

            If td.innerText Like "*(advanced profit centre selection)*" Then

            Set a = td.getElementsByTagName("a")

                    a(0).Click

                    GetIEbyTitle (sTitle)

                    Set ie = CreateObject("InternetExplorer.Application")

                        Sheets("Sheet1").Select

                        With ActiveSheet

                            arr = WorksheetFunction.Transpose(.Range(.[A3], .Cells(Rows.Count, "A").End(xlUp)))

                            If Not IsArray(arr) Then arr = Array(arr)

                            Set itm = ie.document.getElementById("pasteProfitCenter")

                            If Not itm Is Nothing Then itm.Value = Join(arr, DELIMITER)

                        End With

                    Exit For

            End If

        End If

    Next

     

    Public Function GetIEbyTitle(sTitle)

    Dim w

     

      Set GetIEPipe = Nothing

      For Each w In CreateObject("shell.application").Windows

        If InStr(1, TypeName(w.document), "htmldocument", vbTextCompare) > 0 Then

          If InStr(1, w.document.Title, sTitle, vbTextCompare) = 1 Then

            Set GetIEbyTitle = w

            Exit For

          End If

        End If

      Next

     

    End Function

     

    The poster’s comments were as follows:  since that anchor's href is a function, not a URL, and the function does not return a reference to the window it opens, you need to connect to that window in some other way.  One way is to use the Shell.Application to search for the window with the appropriate title, once it has opened, but you need to know the title.  I think he’s right!  The href is NOT really a URL!  I think that’s the whole problem here.  Somehow I have to control that object, without referencing it via a URL.  Not sure how to go about that!!  Any idea on that or have we exhausted all possible options at this point????  I'm definitely out of my confort zone now!!!!

    Sunday, January 16, 2011 9:51 PM
  • I tried this:

    If InStr(1, TypeName(w.document), "MOAP Report Generator", vbTextCompare) > 0 Then

    . . . . but now I'm still getting that dreded error message

    Error message is: ‘Run-time error 2147467259’

    Method ‘Document’ of object ‘IWebBrowser2’ failed

     

    Sunday, January 16, 2011 11:08 PM
  • You need ot let me know what the HREF is set to.  Sometimes it is a URL, and other times it is a script like JAVA.  I disagree with using a SHELL function to run the script.  You should just be able to activate the button/form just like you would with the mouse if you manually performed the action.

     

    I would go back and use my Dump function to help you better understand what the webpage is really doing.  did you try submitting the Form?


    jdweng
    Monday, January 17, 2011 1:18 AM