PDA

View Full Version : Solved: auto login



gsouza
08-28-2006, 07:34 AM
I am trying to auto login to this sight http://www1.mscdirect.com/cgi/nnsrhmSub


IE_login()
Dim ie As InternetExplorer
Dim C
Dim ULogin As Boolean, ieForm
Dim MyPass As String, MyLogin As String
redo:
MyLogin = Application.InputBox("Please enter your VBAX login", "VBAX username", Default:="login", Type:=2)
MyPass = Application.InputBox("Please enter your VBAX password", "VBAX Password", Default:="Password", Type:=2)
If MyLogin = "" Or MyPass = "" Then GoTo redo
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://www1.mscdirect.com/cgi/nnsrhm"
'Loop until ie page is fully loaded
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
'Look for password Form by finding test "Password"
For Each ieForm In ie.Document.forms
If InStr(ieForm.innertext, "Password") <> 0 Then
ULogin = True
'enter details
ieForm(0),Value = MyLogin
ieForm(1).Value = MyPass
'login
ieForm.submit
Exit For
Else
End If
Next
If ULogin = False Then MsgBox "User is aleady logged in"
Set ie = Nothing
End Sub

I can't seem to get the text of the input boxes into the proper fields on the sight. I can't seem to get the text of the input boxes to populate any feilds in that sight. Can anybody help me auto login.

Thank you

mvidas
08-28-2006, 07:57 AM
Hello again,

Looking at the source for that page, the login form is named "f"
Using that, you can make it much easier:Sub IE_login()
Dim ie As InternetExplorer, ieForm As Object
Dim MyPass As String, MyLogin As String
Do
MyLogin = Application.InputBox("Please enter your VBAX login", _
"VBAX username", Default:="login", type:=2)
MyPass = Application.InputBox("Please enter your VBAX password", _
"VBAX Password", Default:="Password", type:=2)
Loop Until Len(MyLogin) > 0 And Len(MyPass) > 0
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://www1.mscdirect.com/cgi/nnsrhm"
'Loop until ie page is fully loaded
Do Until ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop

On Error Resume Next
'I'm guessing the form wont be there if already logged in
Set ieForm = ie.Document.Forms("f")
On Error GoTo 0
If ieForm Is Nothing Then
MsgBox "User is aleady logged in"
Else
ieForm.Item("SIUSER").Value = MyLogin
ieForm.Item("SIPASS").Value = MyPass
ieForm.Submit
End If
Set ie = Nothing
End SubMatt

mvidas
08-28-2006, 08:01 AM
Also, if the "f" form will always be there, you can remove the ieForm variable using a With block. Additionally, you don't need the application.inputboxes there, you can use standard ones if you just want text anyways. Lastly, if you want to give this code to someone that doesnt have the InternetExplorer reference checked, you can use late binding to do the same thing:Sub IE_login()
Dim IE As Object, MyPass As String, MyLogin As String
Do
MyLogin = InputBox("Please enter your username", "Enter username", "Username")
MyPass = InputBox("Please enter your password", "Enter Password", "Password")

Loop Until Len(MyLogin) > 0 And Len(MyPass) > 0
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate "http://www1.mscdirect.com/cgi/nnsrhm"
'Loop until ie page is fully loaded
Do Until IE.readyState = 4 '4=READYSTATE_COMPLETE
DoEvents
Loop
With IE.Document.Forms("f")
.Item("SIUSER").Value = MyLogin
.Item("SIPASS").Value = MyPass
.Submit
End With
Set IE = Nothing
End SubMatt

gsouza
08-28-2006, 08:04 AM
OH thank you so much, I just could not get my hands around it at all. thanks again, i will mark this as solved.

mvidas
08-28-2006, 08:12 AM
Glad to help! Please don't hesitate to ask if you have any questions