Help with Automating IE Login using VBA

andrewj7

New Member
Joined
Jan 20, 2014
Messages
1
Hey Guys,

I'm having some trouble getting my script to work. What I'm trying to do is get this to open up an IE window, prompt the user to enter their username and password, and then have my script automatically fill in the username and password fields, and click submit.

I know that the submit part won't work right now, but I'm mainly concerned with the following error I keep getting:

Method 'Document' of object 'IWebBrowser2' failed

This occurs at the line that I bolded.

Can anyone see what might be wrong with this?

Thanks in Advance!

Code:

Sub OpenCIR()
Dim appIE As Object
Dim sURL As String
Dim UserN As String
Dim PW As String
Dim Element As Object
Dim btnInput As Object
Dim ElementCol As Object
Dim Link As Object
Dim strCountBody As String
Dim lStartPos As Long
Dim lEndPos As Long
Dim TextIWant As String
UserN = InputBox("Please enter your username")
PW = InputBox("Please enter your password")
Application.ScreenUpdating = False
Set appIE = CreateObject("InternetExplorer.Application")
With appIE
.Navigate "http://wsmmart.itg.ti.com/"
.Visible = True
End With
Do While appIE.Busy
Loop
Set doc = appIE.document <--- problem here
Set LoginForm = doc.forms(0)
LoginForm.getElementById("fld2").Value = UserN
LoginForm.getElementById("fld5").Value = PW
Set ElementCol = appIE.document.getElementByTagName("Submit")
For Each btnInput In ElementCol
If btnInput.Value = "Submit" Then
btnInput.Click
Exit For
End If
Next btnInput
Do While appIE.Busy
Loop
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Andrew,

I've used this code in the past and it worked like a charm. The main difference I see with your code is the "wait for the browser to load all info" bit.

Code:
Do
  DoEvents
Loop While IEObj.readyState <> 4 Or IEObj.Busy = True

And a login/logout function:
Code:
Function GetIEAndLogIn() As Object

    Set GetIEAndLogIn = CreateObject("InternetExplorer.Application")
    GetIEAndLogIn.Visible = True
    GetIEAndLogIn.navigate "http://producers-and-traders.de/log.php"
    GetIEAndLogIn.AddressBar = True
    Do Until GetIEAndLogIn.readyState = 4
        DoEvents
    Loop
            
    If GetIEAndLogIn.LocationURL <> "http://producers-and-traders.de/log.php" Then
        'Already logged in
    Else
        Set LogFrm = GetIEAndLogIn.document.forms(0)
        LogFrm.Item("Nickname").Value = "HereGoesMyNickname"
        LogFrm.Item("Passwort").Value = "AndMyPasswordGoesHere"
        LogFrm.Item("login").Click
        GetIEAndLogIn.document.all.Item("login").Click
        LogFrm.submit
    End If
        
End Function

And logging out:

Code:
Function LogIEOut(IE As Object)

    'Do Actions
    IE.navigate "http://producers-and-traders.de/log.php?action=logout"
    IE.Quit
    Set IE = Nothing

End Function

Hope that helps,

Koen
 
Upvote 0

Forum statistics

Threads
1,215,869
Messages
6,127,414
Members
449,382
Latest member
DonnaRisso

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top