Auto fill web forms using VBA

Posted on 2007-10-08
Last Modified: 2013-11-27
I need to be able to go to a website and have VBA automatically enter the Login, Password and client code (all of which I have) and move to the next page.

The following code  navigates there correctly but I have been unable to  fill in the fields either directly or with send keys.

    Set ieApp = CreateObject("InternetExplorer.Application")
    ieApp.Visible = True
    ieApp.Navigate ",349918,,19383973,"

Any ideas would be greatly appreciated.
Question by:marshalldavis
    LVL 1

    Accepted Solution

    This may not be the best code, but it works for me.  

    First set a reference to "Microsoft Internet Controls" (C:\WINDOWS\system32\shdocvw.dll).  But using CreateObject should do essentially the same thing.

    The below code works with the page you provided.  You have to view the page source to get the names of the controls you want to fill.  

    Dim ieApp As InternetExplorer
    Dim iePage As HTMLDocument
    Set ieApp = New InternetExplorer
    ieApp.Visible = True
    ieApp.Navigate ",349918,,19383973,"
    'wait for page to load
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE
    Set iePage = ieApp.Document
    iePage.Forms(0).Item("login").Value = "LOGIN"
    iePage.Forms(0).Item("key").Value = "PASSWORD"
    iePage.Forms(0).Item("clcode").Value = "CLIENT_CODE"

    Author Comment


    Your code works like a charm.  If I can push my luck on the second page (after the login) there is a single button to verify that you wish to enter the system.

    The HTML for the page is:

    <HTML><HEAD><link rel ="stylesheet" type="text/css" href="/styles/default.css">
    <TITLE>TestSystemName</TITLE></HEAD><BODY BGCOLOR=F9F9F9 TEXT=000000 onLoad='SetFocus()'><SCRIPT LANGUAGE="JavaScript">
                var IsForm = false;
                var FirstField;
                function SetFocus() {
                      if(IsForm) {
                            if(FirstField) {
                                  var ind = FirstField.indexOf('document.',0);
                                  if(ind == 0)
                                        var Code = "document.forms[0]."+FirstField+".focus();";
                            } else {
                                  var Cnt = 0;                        
                                  while(document.forms[0].elements[Cnt] != null) {
                                        if(document.forms[0].elements[Cnt].type != "hidden") {
                                        Cnt += 1;
    <!-ShowPage(GENERICCDISPLAY)-> <P>To accept charges shown below, click on the 'View Document' button, otherwise click the 'Back' button on your browser.</P><FORM ENCTYPE='multipart/form-data' method=POST ACTION="/cgi-bin/show_case_doc?28,349918,,19383973,,1"><HR><CENTER><TABLE BORDER=1 BGCOLOR=white width="400"><TR><TH COLSPAN=4><FONT SIZE=+1 COLOR=DARKRED>Pacer Service Center </FONT></TH></TR><TR><TH COLSPAN=4><FONT COLOR=DARKBLUE>Transaction Receipt </FONT></TH></TR><TR></TR><TR></TR><TR><TD COLSPAN=4 ALIGN=CENTER><FONT SIZE=-1 COLOR=DARKBLUE>Thu Oct 11 09:47:32 EDT 2007</FONT></TD></TR><TR><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Pacer Login: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> cw0133 </FONT></TH></TD><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Client Code: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> system                           </FONT></TD></TR><TR><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Description: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Image28-0 </FONT></TD><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Case Number: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> 05-71193-pwb </FONT></TD></TR><TR><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Billable Pages: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> 2 </FONT></TD><TH ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> Cost: </FONT></TH><TD ALIGN=LEFT><FONT SIZE=-1 COLOR=DARKBLUE> 0.16 </FONT></TD></TR><TR></TR><TR></TR></TABLE></CENTER></HR><INPUT type=submit Value="View Document"></FORM>

    Can I get the system to automatically click that button as well?

    Author Comment

    Ended up using


    to submit the second page

    Expert Comment

    Is there any way to change list box containts & require event shold fire. Because in some web page it will display content of second list box only after fist list box value has selected and just passing value is not working.
    E.g. After Selecting State in List Box of State the wab Page fill list of city in List Box of City and if we just change value of state using iePage.Forms(0).Item("State").Value = "STATENAME" then it is not filling city detail.
    Is there any way to fire require event after filling details.

    Sanjay Shah

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now