运行时错误' 91':对象变量或在URL数据脚本在VBA

时间:2017-02-15 10:36:37

标签: excel vba excel-vba

我正在

  

运行时错误' 91':对象变量或With block变量未设置

URL数据脚本

我在我的代码结尾处收到此错误,我应该执行我的代码,即popup.click。

有一件事我注意到如果我在 F8 的帮助下一步一步地运行下面的代码我没有遇到这样的错误但是如果我在<的帮助下执行代码我将面临这个错误strong>运行按钮。

还有一件事我只想提一下,当我用运行按钮执行代码时,弹出值显示没有任何内容,因为我检查。

        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 

2 个答案:

答案 0 :(得分:0)

试试这个,如果你有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

答案 1 :(得分:0)

我已经注意到执行事件的问题所以我已经更改了相同的内容并尝试了以下代码,该代码运行正常。

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