gpt4 book ai didi

VBA:循环中与 IE.doc 引用不一致的错误 91

转载 作者:行者123 更新时间:2023-12-02 09:34:46 25 4
gpt4 key购买 nike

我承认我对 HTML 对象库不太有经验。

我有一个包含 IRS 雇主识别号的电子表格,我必须确定该电子表格是否在我的数据库中。我只能通过网络访问该数据库,其他人已经向该数据库编写了 HTML 并管理该数据库。我认为他们的方法已经过时,设计实践也很差劲;但我最终并不是一名数据库管理员,那么我知道什么呢?因此,我的正常做法是在搜索页面上输入 EIN 并记下结果。

我的 Excel 宏的目的是

  1. 登录基于网络的数据库查询站点。

  2. 循环访问 EIN,记录找到的 EIN

但是,我有以下问题:

  • A.登录部分运行良好,但有一个怪癖:我必须离开验证登录是否成功(或失败)的“If then Else”entact,否则登录失败。鉴于“If then Else”发生登录后,这完全是莫名其妙。
  • B.判断 EIN 是否在数据库中的唯一方法是查看insideText 并查看 EIN 是否出现在以下结果的页面上查询。这是行不通的,即我只有在以下情况下才会得到积极的打击:(在测试中)我连续查询同一个 EIN 两次。 (我在第二个 EIN。)
  • C.在循环中,我得到不一致的错误 91(对象变量不是放)。有时循环会完成;有时循环会完成。有时它会挂起,但永远不会同一个地方。

我的代码如下(尽管我必须更改 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

有什么建议吗?

最佳答案

我发现使用以下“等待 IE 准备就绪”可以取得更好的成功

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function IEWait(p_ieExp As InternetExplorer)

'this should go from ready-busy-ready
Dim initialReadyState As Integer
initialReadyState = p_ieExp.ReadyState

'wait 250 ms until it's done
Do While p_ieExp.Busy Or p_ieExp.ReadyState <> READYSTATE_COMPLETE
Sleep 250
Loop

End Function

你会这样调用它

IEWait IE   'your internet explorer is named "IE"

仅使用“就绪”条件之一时,我遇到了太多奇怪的错误。将我的“就绪”检查修改为该方法后,这种情况几乎 100% 消失了。有时就绪状态并不能准确反射(reflect)状态。

关于您的第一个问题,使用我上面引用的 Sleep 方法,尝试在每个命令之前添加 Sleep 1000 左右,以验证问题是否存在于您的逻辑中,并且在 IE 中加载速度不会太慢。或者使用调试器慢慢地单步调试。

您所描述的内容听起来与我在 IE 部分加载并且我的代码将继续执行时遇到的一些问题非常相似。

关于VBA:循环中与 IE.doc 引用不一致的错误 91,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com