运行时错误'91':对象variables或未设置块variables而URL数据在VBA中编写脚本

我正进入(状态

运行时错误'91':对象variables或块variables未设置

而URL数据脚本

我得到这个错误在我的代码,我应该执行我的代码,即popup.click结束。

有一件事我注意到,如果我在F8的帮助下一步一步地运行下面的代码,我没有面对这样的错误,但是我面对这个错误,如果我在运行button的帮助下执行代码。

还有一件事我只想提到,当我执行代码与运行buttonpopup窗口值没有显示,因为我检查。

Sub GoToWebsiteTest() Dim appIE As InternetExplorerMedium Dim tags As Object Dim tagx As Object Dim repo As Object Dim popup As Object Dim Element As HTMLButtonElement Dim MyHTML_Element As IHTMLElement Dim HTMLdoc As HTMLDocument i = 0 Set appIE = New InternetExplorerMedium sURL = "https://oneview.myurl.in/" With appIE .navigate sURL .Visible = True End With Do While appIE.Busy Or appIE.readyState <> 4 DoEvents Loop appIE.document.getElementById("Username").Value = "xxx" appIE.document.getElementById("Password").Value = "xxxx" i = InputBox("type") appIE.document.getElementById("txtcaptcha").Value = i Set tags = appIE.document.getElementsByTagName("input") For Each tagx In tags If tagx.Type = "submit" Then tagx.Click End If Next Do While appIE.Busy Or appIE.readyState <> 4 DoEvents Loop Set popup = appIE.document.getElementById("ctl00_MainContent_gridUser_ctl00_ctl04_lnkG2") ' Here I am getting Object variable or With block variable not set error popup.Click 

试试这个,让我知道如果你有MsgBox或如果它的工作:

 Sub GoToWebsiteTest() Dim appIE As InternetExplorerMedium Dim tags As Object Dim tagx As Object Dim repo As Object Dim popup As Object Dim Element As HTMLButtonElement Dim MyHTML_Element As IHTMLElement Dim HTMLdoc As HTMLDocument i = 0 Set appIE = New InternetExplorerMedium sURL = "https://oneview.myurl.in/" With appIE .navigate sURL .Visible = True End With Do While appIE.Busy Or appIE.readyState <> 4 DoEvents Loop appIE.document.getElementById("Username").Value = "xxx" appIE.document.getElementById("Password").Value = "xxxx" i = InputBox("type") appIE.document.getElementById("txtcaptcha").Value = i Set tags = appIE.document.getElementsByTagName("input") For Each tagx In tags If tagx.Type = "submit" Then tagx.Click End If Next Do While appIE.Busy Or appIE.readyState <> 4 DoEvents Loop Set popup = appIE.document.getElementById("ctl00_MainContent_gridUser_ctl00_ctl04_lnkG2") DoEvents If Not popup Is Nothing Then popup.Click Else MsgBox "popup is not defined!", vbOKOnly + vbCritical End If '''rest of your code End Sub 

我已经注意到与事件的问题所以我已经改变了一样,并尝试下面的代码,这是工作正常。

 Sub GoToWebsiteTest() Dim appIE As InternetExplorerMedium Dim tags As Object Dim tagx As Object Dim repo As Object Dim Element As HTMLButtonElement Dim MyHTML_Element As IHTMLElement Dim HTMLdoc As HTMLDocument i = 0 Set appIE = New InternetExplorerMedium sURL = "https://oneview.myurl.in/" With appIE .navigate sURL .Visible = True End With Do While appIE.Busy Or appIE.readyState <> 4 DoEvents Loop appIE.document.getElementById("Username").Value = "xxx" appIE.document.getElementById("Password").Value = "xxxx" i = InputBox("type") appIE.document.getElementById("txtcaptcha").Value = i Set tags = appIE.document.getElementsByTagName("input") For Each tagx In tags If tagx.Type = "submit" Then tagx.Click End If Next Application.Wait (Now + TimeValue("00:00:05")) Dim popup As Object Set popup = appIE.document.getElementById("ctl00_MainContent_gridUser_ctl00_ctl04_lnkG2") popup.Click