VBA : Internet: Login to a site using the Internet Explorer Application, controlled from VBA 
Options: Save as PDF | Save attached file | Toggle line numbers
| Type: |
sub |
| Added By: |
Rembo |
| Short Description: |
This sub routine shows how to use the Internet Explorer Application from VBA. |
| Notes: |
For this routine you need a webpage to login to, for instance a webmail login page. |
| Added: |
Sep 28 2005 at 9:02 PM |
| Modified: |
Dec 28 2005 at 10:45 PM |
| Related URLs |
http://www.mrexcel.com/board2/viewtopic.php?p=821822
|
Let's say you have a login page at http://www.yourdomain.com/yourloginpage.html
After clicking the Submit button it has to take you to the page
https://www.yourdomain.com/loggedin.php
Normally the source code of such a page would look something like this:
<head>
<title>Example webpage with form</title>
</head>
<body>
<form name="yourFormname" action="https://www.yourdomain.com/loggedin.php" method="post">
Your name: <input type="text" value="" name="loginname"><br>
Password: <input type="password" value="" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
Notice that:
the form name is 'yourFormname'
the login field name is 'loginname'
the password field name is 'password'
Sub OpenWebpageAndLogin()
Dim objIE As Object
On Error GoTo error_handler
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Navigate "http://www.yourdomain.com/yourpage.html"
Do While .Busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
.Visible = True
With .Document.forms("yourFormname")
.loginname.Value = "your_loginname"
.password.Value = "your_password"
.submit
' Note: depending on scripts it's sometimes s necessary to
' use .submit.Click instead of just .Submit
End With
End With
Set objIE = Nothing
Exit Sub
error_handler:
MsgBox ("Unexpected Error, I'm quitting.")
objIE.Quit
Set objIE = Nothing
End Sub