getElementById rutine error 91

时间:2017-06-14 05:41:30

标签: vba

我有VBA代码登录FB。但是当我开始时我有一个

  

运行时错误91“对象变量或未设置块变量”。

然后我调试此刻一切正常。

请帮帮我。

Option Explicit

Sub FB_Login()
    Dim Site As Object
    Set Site = CreateObject("InternetExplorer.application")
    Dim FB_ID As String
    Dim FB_PW As String
    Dim URL As String
    Dim oHTMLDoc As Object

    FB_ID = InputBox("Podaj adress e-mail do logowania")
    FB_PW = InputBox("Podaj Hasło FB")

    Site.Visible = False

    URL = "Facebook.com"

    Site.navigate URL
    While Site.busy
    Wend

    Set oHTMLDoc = Site.document

    oHTMLDoc.getElementById("email").Value = ""    'Gives error
    oHTMLDoc.getElementById("email").Value = FB_ID    'Gives error
    oHTMLDoc.getElementById("pass").Value = FB_PW  'Run-time error '424'
    oHTMLDoc.getElementById("loginbutton").Click   'Object required

    While Site.busy
    Wend
    Site.Visible = True
End Sub

1 个答案:

答案 0 :(得分:0)

在下面尝试此代码:

创建一个模块并将此代码放在下面:

Public Site As New InternetExplorer


这是您的新fb_login函数:

    Dim FB_ID As String
    Dim FB_PW As String
    Dim URL As String
    Dim oHTMLDoc As Object


    URL = "Facebook.com"

    FB_ID = InputBox("Podaj adress e-mail do logowania")
    FB_PW = InputBox("Podaj Haslo FB")


    Site.Navigate2 URL
    Site.Visible = True

    WaitWhileBrowserIsLoading 1500

    Set oHTMLDoc = Site.document

    oHTMLDoc.getElementById("email").Value = FB_ID
    oHTMLDoc.getElementById("pass").Value = FB_PW
    oHTMLDoc.getElementById("loginbutton").Click


在下面包含此功能以检查浏览器的状态:

Public Sub WaitWhileBrowserIsLoading(Optional intWaitForBrowserToBecomeBusy As Integer = 0, Optional timeOutSeconds As Double = 0)
    With Site
        'wait for the browser to become busy to avoid skipping through without checking
        If intWaitForBrowserToBecomeBusy = 0 Then
            Sleep 800
        Else
            Sleep intWaitForBrowserToBecomeBusy
        End If

        Dim startTime As Double
        startTime = Timer
        Dim timePassed As Double
        timePassed = 0
        'Wait IE
        Do While .readyState <> READYSTATE_COMPLETE
            Sleep 100
            DoEvents
            If timeOutSeconds <> 0 Then
                timePassed = Timer - startTime
                If timePassed > timeOutSeconds Then Exit Sub
            End If
        Loop
        'Wait document
        Do While .document.readyState <> "complete"
            Sleep 100
        Loop
    End With
End Sub