PDA

View Full Version : IE login and logout from Excel



gaup
08-26-2009, 11:28 AM
Hi all and thanks in advance for your help with my question.

I need to download data from a website to an Excel file that I need to log on with username and password. Those username/password are shared by several people, only one can be logged on at any time. What I need to do then each time is to make sure that I am completely logged out, then log in, then once done log out again. Therefore I have two subs, one for login and the other for logout.

The macro was originally generated for Excel 2003. Back then you needed to manually log in before you run the data downloading procedures in Excel. We switched to Excel 2007 and that manual approach did not work and I was asked to fix it. I decided to incorporate the login/out into the macro so to eliminate that manual step. The codes work on my computer flawlessly but does not on other machines running the same IE7 and Office 2007. There are a couple of error messages but the most common one is:

Iwebbrowser2 method invalid

for ie.Busy

I do not declare Iwebbrowser2 anywhere in the proc.

Kindly let me know how best to fix this. Thanks.

Public Sub Logout()

Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "?????????.com/scripts/logout.php?return_url=/)"
End With
Do While ie.Busy And Not ie.ReadyState = 4
DoEvents
Loop
ie.Visible = True
Application.Wait Now + TimeSerial(0, 0, 2)
ie.Quit
Set ie = Nothing

End Sub

Public Sub Login()

Dim ipf As Object
Dim user As String
Dim pass As String
user = Sheets("Sheet1").Range("C1").Value
pass = Sheets("Sheet1").Range("C2").Value

Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "?????????.com/"
Do Until .ReadyState = 4
DoEvents
Loop

Set ipf = .Document.all.Item("username")
ipf.Value = user
Set ipf = .Document.all.Item("password")
ipf.Value = pass
Set ipf = .Document.all.Item("global_login_loginbutton")
Do Until .ReadyState = 4
DoEvents
Loop
ipf.Click

End With

End Sub