VBA: Niespójny błąd 91 w odwołaniu do pętli w / IE.doc

Przyznaję, że nie jestem zbyt doświadczony w bibliotece obiektów HTML.

Mam arkusz kalkulacyjny z numerami identyfikacyjnymi pracodawcy IRS, które muszę zidentyfikować jako znajdujące się w mojej bazie danych. Mam tylko dostęp internetowy do tej bazy danych, do której inni napisali HTML i zarządzają bazą danych. Wierzę, że ich metody są przestarzałe, a praktyki projektowe słabe; ale nie jestem ostatecznie menedżerem baz danych, więc co ja wiem? W związku z tym normalną praktyką jest wpisanie EIN na stronie wyszukiwania i zanotowanie wyniku.

Moje makro Excel ma na celu

Zaloguj się do internetowej witryny zapytań bazy danych.

Przeprowadź pętlę przez EIN, zaznaczając, które EIN są znalezione

Mam jednak następujące problemy:

O. Część logowania działa dobrze, ale z jednym dziwactwem: muszę zostawić „Jeśli wtedy jeszcze”, że weryfikacja logowania zakończyła się powodzeniem (lub nie), w przeciwnym razie logowanie nie powiedzie się. Biorąc pod uwagę, że po zalogowaniu pojawia się „If Then Else”, jest to całkowicie zaskakujące.B. Jedynym sposobem na sprawdzenie, czy EIN znajduje się w bazie danych, jest sprawdzenie wewnętrznego tekstu i sprawdzenie, czy EIN występuje na stronie wynikającej z zapytania. To nie działa, tzn. Dostaję tylko pozytywne trafienie, gdy (w testowaniu) dwa razy z rzędu sprawdzam ten sam EIN. (Dostaję trafienie w drugim EIN.)C. W pętli otrzymuję niespójne błędy 91 (nie ustawiona zmienna obiektu). Czasami pętla się kończy; czasem się zawiesza, ale nigdy w tym samym miejscu.

Mój kod jest następujący (chociaż musiałem zmienić adresy URL):

Option Explicit
Sub FillFromWorkbookTest()

On Error GoTo ErrHandler

Const cURL = "https://www.someURL.com/LoginPage.jsp"
Const cUsername = "myUSERNAME"
Const cPassword = "myPASSWORD"
Dim IE As Object
Dim Doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInput As HTMLInputElement
Dim PasswordInput As HTMLInputElement
Dim LoginButton As HTMLInputButtonElement
Dim SearchForm As HTMLFormElement
Dim EINInput As HTMLInputElement
Dim SearchButton As HTMLInputButtonElement
Dim cEIN As String
Dim BotRow As Long
Dim EINRange As Range
Dim c As Variant
Dim i As Integer
Dim EINCheck As String
Dim EINCount As Integer

'## Open Browser & go to Admin Module, and Login
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate cURL

'## Wait for Adimn Module to load
Do Until IE.ReadyState = 4
    DoEvents
Loop

'## Get the HTML Document of Admin Module login page (cURL)
Set Doc = IE.document

'## Get Admin Module login form
Set LoginForm = Doc.forms("f1")

'## Get Username input field and populate it
'## HTML: <input id=EIN type=text tabindex=3 size=9 maxlength=9 name=EIN title="Admin Code">
Set UsernameInput = LoginForm.elements("EIN")
UsernameInput.Value = cUsername

'## Get Password input field and populate it
'## HTML: <input id=PIN type=password tabindex=4 size=8 maxlength=8 name=PIN title="PIN">
Set PasswordInput = LoginForm.elements("PIN")
PasswordInput.Value = cPassword

'## Submit LoginForm
'## HTML: <input type=submit value=Login tabindex=5 title="Login"> (no onClick attribute; no element)
LoginForm.submit

Do Until IE.ReadyState = 4
    DoEvents
Loop

'## Get the HTML Document of the new page
Set Doc = IE.document

'## Determine if login succeeded
If InStr(Doc.body.innerText, "Invalid Login.") = 0 Then
    MsgBox "Login successful."
Else
    MsgBox "Login failed."
End If

Debug.Print "Current URL: " & IE.LocationURL

'## Navigate to Global Change and reset HTML Document
IE.Navigate "https://www.someURL.com/LOGGED_IN/SomePage.jsp"

Do Until IE.ReadyState = 4
    DoEvents
Loop

Set Doc = IE.document

'## Find last row in spreadsheet
BotRow = Worksheets("Sheet1").Range("A1").End(xlDown).Row
Set EINRange = Range("A1:A" & BotRow)

'## Set loop counter variable
i = 0

'## Cycle through IRS-identified EINs
For Each c In EINRange.Cells

    cEIN = c.Value
    i = i + 1

'## Get Admin Module login form
    Set SearchForm = Doc.forms(0)

'## Get EIN input field and populate it
'## HTML: <input type="text" id=EIN name=EIN title="Enter charity EIN" maxlength=9 size=9 tabindex=11 >
    Set EINInput = SearchForm.elements("EIN")
    EINInput.Value = cEIN

'## Submit SearchForm
'## HTML: <input type="submit" value="Search" tabindex=15 title="Click here to search charity application" class="black_bold"
'##       onclick="if (btn_OnClick(EIN,CODE)) {document.f1.action='SomeOther.jsp'; document.f1.submit(); return true;} else return false;" >
'##       (has onClick attribute)

    Set SearchButton = Doc.body.getElementsByTagName("table")(2). _
        getElementsByTagName("tr")(0). _
        getElementsByTagName("td")(0). _
        getElementsByTagName("input")(2)
    SearchButton.Click

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

'## Get the HTML Document of the new page
    Set Doc = IE.document

'## Find EIN string on resulting page; Some number if found; Null if not
    EINCheck = Doc.body.getElementsByTagName("table")(3).innerText
    EINCount = InStr(1, EINCheck, cEIN, 1)
    MsgBox EINCount 

'## Determine which EINs are CFC charities
    If InStr(1, EINCheck, cEIN, 1) = 0 Then
        Worksheets("Sheet1").Range("F" & i).Value = "NO"
    Else
        Worksheets("Sheet1").Range("F" & i).Value = "YES"
    End If

Next c

ErrHandler:
'## Cleanup
MsgBox "Error" & Err.Number & ": " & Err.Description
Set IE = Nothing
Set Doc = Nothing
Set LoginForm = Nothing
Set UsernameInput = Nothing
Set PasswordInput = Nothing
Set LoginButton = Nothing
Set SearchForm = Nothing
Set EINInput = Nothing
Set SearchButton = Nothing

End Sub

Jakieś sugestie?

questionAnswers(1)

yourAnswerToTheQuestion