Keep reference to object created with Internet Explorer

时间:2016-08-31 18:12:40

标签: excel vba excel-vba

I am trying to login to a website and then continue clicking on and filling in information within that same website. However, when I run my code I run into an error that says the object is forgotten.

Here is the code

Sub Vba2HtmlForm()
    Dim IE as Object
    Dim frm as variant
    dim element as variant

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "https://www.programworkshop.com/6.0.0.0/login/login.aspx?skin=default&lang=enu"
    While IE.readyState <> 4: DoEvents: Wend

    ''trying to get form by ID''

    IE.document.getElementById("Login").Value = "something@something.com"

    IE.document.getElementById("Pass").Value = "somepassword"
    IE.document.getElementById("btnlogin").Click
    While IE.readyState <> 4: DoEvents: Wend

     Application.Wait (Now + TimeValue("00:00:02"))

    ' And then here is where I run into the following error:
    ' Run-time error '424':
    ' Object Required

    IE.document.all("sd5").Click
    '''just a note here. I have also tried getelementbyId and getelementbyname with no luck''''

End Sub

Here is the HTML

<a id="sd5" class="node" href="javascript:treeAction(strURL5)" onmouseover="window.status='Examinees';return true;" onmouseout="window.status='';return true;" onclick="javascript: d.s(5);">Examinees</a>

1 个答案:

答案 0 :(得分:0)

After you navigate is when it Loses its place. You can use a small method to obtain the correct IE object by passing in the URL you navigated to, assuming it isn't redirected and only one instance of IE has the url. You will need to add a reference to Microsoft Shell Controls and Automation.

Call it like

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.programworkshop.com/6.0.0.0/login/login.aspx?skin=default&lang=enu"
While IE.readyState <> 4: DoEvents: Wend
set IE = GetWebPage("https://www.programworkshop.com/6.0.0.0/login/login.aspx?skin=default&lang=enu")



''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
'   URL from the sURL Parameter.  The Function Timesout after 30 seconds
'Input parameters:
    'String sURL - The URL to look for
'Output parameters:
    'InternetExplorer ieWin - the Internet Explorer window holding the webpage
'Result: returns the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetWebPage(sURL As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
Dim ieWin As InternetExplorer

dt = DateAdd("s", 30, DateTime.Now)

Do While dt > DateTime.Now
    Set winShell = New Shell
    'loop through the windows and check the internet explorer windows
    For Each ieWin In winShell.Windows
        If ieWin.LocationURL = sURL Then
            ieWin.Visible = True
            Set GetWebPage = ieWin
            Do While ieWin.Busy
                DoEvents
            Loop
            Exit Do
        Set winShell = Nothing
        End If
    Next ieWin
    Set winShell = Nothing
    DoEvents
Loop
End Function