VBA 运行-time error '91' - Object variable or With block variables not set - IE Button click error
VBA Run-time error '91' - Object variable or With block variables not set - IE Button click error
我的脚本需要帮助。我是 VBA 的新手,我已经编写了以下代码来加载 Intranet 网页并登录。登录后,下一步是单击网页上的几个按钮之一。然而,我从下面粗体文本的最后一行代码中得到 运行-time error 91 (IE1.document.getElementById("btnAddPerson").Click)。
你能告诉我这里有什么问题吗?
谢谢。
问候
迈克尔 T
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Option Explicit
Private Declare Function ShowWindow _
Lib "user32" _
(ByVal HWND As Long, _
ByVal nCmdShow As Long) _
As Long
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows
Dim win As Object, rv As SHDocVw.InternetExplorer
For Each win In objShellWindows
If TypeName(win.document) = "HTMLDocument" Then
If UCase(win.document.Title) = UCase(sTitle) Then
Set rv = win
Exit For
End If
End If
Next
Set IEWindowFromTitle = rv
End Function
Sub Test1()
'Login into IE
Dim IE As Object
Dim IE1 As Object
Dim doc As HTMLDocument
Dim btn As HTMLButtonElement
Dim MyHTML_Element As IHTMLElement
Dim MyHTML_Element1 As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
Set IE1 = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://some_intranet_url/1"
Do While IE.Busy
ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementById("username").Value = "some_username"
doc.getElementById("password").Value = "some_password"
'Click to Login
For Each MyHTML_Element In doc.getElementById("loginForm")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
Do While IE.Busy
ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
'Selct role
doc.getElementById("selectedRole").Value = "555885740104"
'Click to Login
doc.getElementById("button1").Click
'Click to add person
Do While IE.Busy
' ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
'Set page to be fully loaded
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Visible = True
IE1.navigate "https://some_intranet_url/2"
Do While IE1.Busy
ShowWindow IE1.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
**IE1.document.getElementById("btnAddPerson").Click**
End Sub
根据你的代码,我用下面的代码创建了一个示例,在我这边效果很好,你也可以检查一下。
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows
Dim win As Object, rv As SHDocVw.InternetExplorer
For Each win In objShellWindows
If TypeName(win.Document) = "HTMLDocument" Then
If UCase(win.Document.Title) = UCase(sTitle) Then
Set rv = win
Exit For
End If
End If
Next
Set IEWindowFromTitle = rv
End Function
Sub Test1()
'Login into IE
Dim IE As Object
Dim IE1 As Object
Dim doc As HTMLDocument
Dim btn As HTMLButtonElement
Dim MyHTML_Element As IHTMLElement
Dim MyHTML_Element1 As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
Set IE1 = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.google.com/"
Do While IE.Busy
'ShowWindow IE.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.Document
'Click to Login
Do While IE.Busy
'ShowWindow IE.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.Busy
' ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
'Set page to be fully loaded
Loop
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Visible = True
IE1.Navigate "https://www.bing.com/"
Do While IE1.Busy
'ShowWindow IE1.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Document.getElementById("sb_form_q").Value = "vba"
IE1.Document.getElementById("sb_form_go").Click
End Sub
由于无法重现您的问题,建议您可以参考以下步骤缩小问题范围,尝试解决。
IE1.Visible = True
IE1.navigate "https://some_intranet_url/2"
Do While IE1.Busy
ShowWindow IE1.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
**IE1.document.getElementById("btnAddPerson").Click**
首先,请检查上面的代码,导航到另一个页面后,我们应该等待 "IE1" 实例完成,而不是 "IE" 实例。请尝试修改代码如下:
IE1.Visible = True
IE1.Navigate "https://www.bing.com/"
Do While IE1.Busy
'ShowWindow IE1.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
其次,导航成功后,请检查焦点window并使用F12开发者工具检查网站资源,确保可以找到"btnAddPerson"按钮。
此外,这里有一篇关于VBA错误91的文章,你可以参考一下:
我的脚本需要帮助。我是 VBA 的新手,我已经编写了以下代码来加载 Intranet 网页并登录。登录后,下一步是单击网页上的几个按钮之一。然而,我从下面粗体文本的最后一行代码中得到 运行-time error 91 (IE1.document.getElementById("btnAddPerson").Click)。
你能告诉我这里有什么问题吗?
谢谢。
问候 迈克尔 T
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Option Explicit
Private Declare Function ShowWindow _
Lib "user32" _
(ByVal HWND As Long, _
ByVal nCmdShow As Long) _
As Long
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows
Dim win As Object, rv As SHDocVw.InternetExplorer
For Each win In objShellWindows
If TypeName(win.document) = "HTMLDocument" Then
If UCase(win.document.Title) = UCase(sTitle) Then
Set rv = win
Exit For
End If
End If
Next
Set IEWindowFromTitle = rv
End Function
Sub Test1()
'Login into IE
Dim IE As Object
Dim IE1 As Object
Dim doc As HTMLDocument
Dim btn As HTMLButtonElement
Dim MyHTML_Element As IHTMLElement
Dim MyHTML_Element1 As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
Set IE1 = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://some_intranet_url/1"
Do While IE.Busy
ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementById("username").Value = "some_username"
doc.getElementById("password").Value = "some_password"
'Click to Login
For Each MyHTML_Element In doc.getElementById("loginForm")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
Do While IE.Busy
ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
'Selct role
doc.getElementById("selectedRole").Value = "555885740104"
'Click to Login
doc.getElementById("button1").Click
'Click to add person
Do While IE.Busy
' ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
'Set page to be fully loaded
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Visible = True
IE1.navigate "https://some_intranet_url/2"
Do While IE1.Busy
ShowWindow IE1.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
**IE1.document.getElementById("btnAddPerson").Click**
End Sub
根据你的代码,我用下面的代码创建了一个示例,在我这边效果很好,你也可以检查一下。
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows
Dim win As Object, rv As SHDocVw.InternetExplorer
For Each win In objShellWindows
If TypeName(win.Document) = "HTMLDocument" Then
If UCase(win.Document.Title) = UCase(sTitle) Then
Set rv = win
Exit For
End If
End If
Next
Set IEWindowFromTitle = rv
End Function
Sub Test1()
'Login into IE
Dim IE As Object
Dim IE1 As Object
Dim doc As HTMLDocument
Dim btn As HTMLButtonElement
Dim MyHTML_Element As IHTMLElement
Dim MyHTML_Element1 As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
Set IE1 = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.google.com/"
Do While IE.Busy
'ShowWindow IE.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.Document
'Click to Login
Do While IE.Busy
'ShowWindow IE.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.Busy
' ShowWindow IE.HWND, 3
Application.Wait DateAdd("s", 1, Now)
'Set page to be fully loaded
Loop
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Visible = True
IE1.Navigate "https://www.bing.com/"
Do While IE1.Busy
'ShowWindow IE1.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE1.Document.getElementById("sb_form_q").Value = "vba"
IE1.Document.getElementById("sb_form_go").Click
End Sub
由于无法重现您的问题,建议您可以参考以下步骤缩小问题范围,尝试解决。
IE1.Visible = True
IE1.navigate "https://some_intranet_url/2"
Do While IE1.Busy
ShowWindow IE1.HWND, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
**IE1.document.getElementById("btnAddPerson").Click**
首先,请检查上面的代码,导航到另一个页面后,我们应该等待 "IE1" 实例完成,而不是 "IE" 实例。请尝试修改代码如下:
IE1.Visible = True
IE1.Navigate "https://www.bing.com/"
Do While IE1.Busy
'ShowWindow IE1.Hwnd, 3
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
其次,导航成功后,请检查焦点window并使用F12开发者工具检查网站资源,确保可以找到"btnAddPerson"按钮。
此外,这里有一篇关于VBA错误91的文章,你可以参考一下: