Scriptorium

us fr nl




VBA : Internet: Login to a site using the Internet Explorer Application, controlled from VBA   us


Options: Save as PDF | Save attached file | Toggle line numbers

Details:

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


Usage:

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'



Code:

Formatted | Unformatted
  1. Sub OpenWebpageAndLogin()
  2. Dim objIE As Object
  3. On Error GoTo error_handler
  4. Set objIE = CreateObject("InternetExplorer.Application")
  5. With objIE
  6. .Navigate "http://www.yourdomain.com/yourpage.html"
  7. Do While .Busy: DoEvents: Loop
  8. Do While .ReadyState <> 4: DoEvents: Loop
  9. .Visible = True
  10. With .Document.forms("yourFormname")
  11. .loginname.Value = "your_loginname"
  12. .password.Value = "your_password"
  13. .submit
  14. ' Note: depending on scripts it's sometimes s necessary to
  15. ' use .submit.Click instead of just .Submit
  16. End With
  17. End With
  18. Set objIE = Nothing
  19. Exit Sub
  20. error_handler:
  21. MsgBox ("Unexpected Error, I'm quitting.")
  22. objIE.Quit
  23. Set objIE = Nothing
  24. End Sub



User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website