简体   繁体   中英

Keep getting run-time error VBA

I am trying to get this to log into my website with a main login page and a second One Time Password(OTP) page. But i am getting and error

run-time error '-2147467259 (80004005)':

Automation error

Unspecified error

found at this line:

 If InStr(wd.document.Title, "Sales Force Automation") <> 0 Then

What am i doing wrong here? I am new to this language as i just started learning it 2 days ago.

Dim wd As SHDocVw.InternetExplorer

Sub login()
    Dim username As Range
    Dim password As Range
    Dim otp As Range
    Dim myValue As Variant

    Set wd = CreateObject("InternetExplorer.Application")
    wd.silent = True
    wd.navigate "Http://www.XXXXXXXXXXX.com"
    wd.Visible = True

    Set username = Range("B1")
    Set password = Range("B2")

    While wd.Busy
        DoEvents
    Wend

    wd.document.all.UserId.Value = username
    wd.document.all.password.Value = password

    Application.Wait (Now + TimeValue("0:00:10"))

    wd.document.all.btnobj.Click

    myValue = InputBox("Enter OTP")
    Range("B3").Value = myValue


Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If

    Call FindTicketWindow
End Sub

Sub FindTicketWindow()
    Dim otp As Range

    For Each wd In CreateObject("Shell.Application").Windows
        If wd = "Internet Explorer" Then
            If InStr(wd.document.Title, "Sales Force Automation") <> 0 Then
                Application.Wait (Now + TimeValue("0:01:00"))
                Exit For
            End If
        End If
    Next wd

    Set otp = Range("B3")

    While wd.Busy
        DoEvents
    Wend

    myVar = wd.document.Title

    wd.document.all.verficationcode.Value = otp
    Application.Wait (Now + TimeValue("0:00:10"))
    wd.document.all.btnobj.Click
End Sub

I have had an issue like this before, what I found was that VBA is trying to do an operation to an object before that object has been created or changed as the code would expect. Hence when stepping through the code work because it has ample time between steps to complete calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM