簡體   English   中英

運行時錯誤'91':在VBA中編寫URL數據腳本時,未設置對象變量或With塊變量

[英]Run Time Error '91': Object variable or With block variable not set while URL data Scripting in VBA

我正進入(狀態

運行時錯誤“ 91”:對象變量或帶塊變量未設置

而URL數據腳本

我在應該執行我的代碼即popup.click的代碼結尾處收到此錯誤。

我注意到,如果我在F8的幫助下一步一步運行此代碼,我沒有遇到這種錯誤,但是如果我借助“ 運行按鈕”執行代碼,就會遇到此錯誤。

我還要提一件事,當我使用“運行按鈕”彈出值執行代碼時,在我檢查時什么也沒有顯示。

        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

我注意到了Do事件的問題,因此我進行了更改,並嘗試了以下運行良好的代碼。

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM