Problem with accessing ie DOM from vba (1 Viewer)

jesse

Registered User.
Local time
Today, 11:53
Joined
Jul 14, 2010
Messages
39
Hi,

I want to automatically log in to a website to get some information of it to load into my database. Below is an attempt to set the value of two textboxes on a login page. Does any body know why this would give the error: "91: objectvariable or with blockvariable not set"?

thanks


Code:
Sub loginSite()
    On Error GoTo errhandler
    Dim ie As InternetExplorer
    Dim docDOM As HTMLDocument
    Dim input_user As HTMLInputElement
    Dim input_pass As HTMLInputElement
    
    Set ie = New InternetExplorer
    
    ie.Navigate ("[URL="http://www/"]http://www[/URL]......")
    
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop
 
    Set docDOM = ie.Document
    
    Set input_user = docDOM.getElementsByName("user").Item(Index:=0)
    Set input_pass = docDOM.getElementsByName("password").Item(Index:=0)
    
    input_user.Value = "JESSE"
    input_pass.Value = "password"
    
       
    ie.Visible = True
    Exit Sub
    
errhandler:
    Debug.Print "error" & Err.Number
    Debug.Print Err.Description
End Sub
 

darbid

Registered User.
Local time
Today, 12:53
Joined
Jun 26, 2008
Messages
1,428
Hi,

I want to automatically log in to a website to get some information of it to load into my database. Below is an attempt to set the value of two textboxes on a login page. Does any body know why this would give the error: "91: objectvariable or with blockvariable not set"?
The webbrowser / Webbrowser control has a document complete event. You cannot set the document until that event has fired.
 

jesse

Registered User.
Local time
Today, 11:53
Joined
Jul 14, 2010
Messages
39
Hi Darbid,

thanks for your response. I didn't know about that event. I gave it a try (see code below). However, I still get the same error message "object variable or with block variable not set". Can you see if there is another problem with my code?

Code:
Sub loginSite()
    On Error GoTo errhandler
    Dim IE As clsIE
    Dim input_user As HTMLInputElement
    Dim input_pass As HTMLInputElement
    Dim docDOM As HTMLDocument
 
    Set IE = New clsIE
 
    IE.Navigate ("[URL="http://www....."]www.....[/URL]")
 
    Do Until IE.DocumentLoaded
        DoEvents
    Loop
 
    Set docDOM = IE.document
 
    Set input_user = docDOM.getElementsByName("user").Item(Index:=0)
    Set input_pass = docDOM.getElementsByName("password").Item(Index:=0)
 
    input_user.Value = "JESSE"
    input_pass.Value = "password"
 
 
    Exit Sub
 
errhandler:
    Debug.Print "error" & Err.Number
    Debug.Print Err.Description
End Sub

class module

Code:
option Compare Database
Option Explicit
 
Dim doc_loaded As Boolean
Dim WithEvents IE As InternetExplorer
 
Private Sub Class_Initialize()
    On Error Resume Next
    IE.Visible = True
    If Err.Number <> 0 Then
        Set IE = New InternetExplorer
        IE.Visible = True
        End If
    End Sub
 
Public Sub Navigate(ByVal URL As String)
    doc_loaded = False
    IE.Navigate URL
    End Sub
 
Private Sub IE_documentcomplete(ByVal pDisp As Object, URL As Variant)
    doc_loaded = True
 
End Sub
 
Public Property Get DocumentLoaded() As Boolean
    DocumentLoaded = doc_loaded
End Property
 
Public Property Get document() As HTMLDocument
    document = IE.document
End Property
 

jesse

Registered User.
Local time
Today, 11:53
Joined
Jul 14, 2010
Messages
39
Oops, forgot to use "set" in "document = IE.document"

works perfectly now, thanks a lot!
 

Users who are viewing this thread

Top Bottom