How to Populate Web Pages in VBA

Use the VBA SendKeys statement to send keystrokes that populate a Web page's controls. VBA has other approaches for populating Web pages, but they involve studying the functions and properties of specialized classes such as "Application.InternetExplorer," which can be time-consuming. Those new to document automation will find SendKeys a more intuitive approach. "Automation" is the term programmers use to refer to one application controlling another. Note that while your program is running, you can't perform any operations on your computer. Doing so would send unwanted keystrokes to the Web page you want to populate.

Instructions

    • 1

      Click the "File" menu's "New" command of any Office application, then click "OK" to create a new document. Press "Alt" and "F11"simultaneously to enter the VBA programming environment, then double-click the "This Document" item in the "Project" pane. This task opens a new window in which you can type or paste a VBA program.

    • 2

      Open in your Web browser a Web page you'd like to populate with VBA. Count the number of "Tab" keypresses required to move to the first control you want to populate. For example, assume the page you chose was the home page of "ConceptArt.org," and the first control you want to populate is the "Username" control. Press "Tab" repeatedly until the cursor enters that text box control. You'll find that two "Tab" keystrokes are needed to move to this control.

    • 3

      Type the following statement in the VBA programming window. This statement activates the Web page you want to populate. This step is necessary because the SendKeys statement sends keystrokes to whichever application window is active. Type over the "ConceptArt.org" sample text with the name that appears in the title bar of the Web page you want to populate.

      Sub populatePage()
      AppActivate ("ConceptArt.org")
      End Sub

    • 4

      Type the following statement in the VBA programming window. This statement moves the current insertion point to the control you want to populate. The statement performs the equivalent of the "Tab" keystrokes you entered manually. Replace the "2" with the number of "Tab" keystrokes needed to move to the control you chose.

      SendKeys "{TAB 2}"

    • 5

      Type the following "SendKeys" statement after the previous one. This statement populates a textbox control with text. You can type over the sample text entered with the text you want to populate the control with.

      SendKeys "myUserName"

    • 6

      Count the number of "Tab" keystrokes needed to move from the previous control you populated to the next control you want to populate. This is the same task as the one needed to move to the first populated control.

    • 7

      Type the "SendKeys" statement that sends the number of "Tab" keystrokes you just counted. Type the "SendKeys" statement that sends the text you want to populate the next control with. For example, if your previous "SendKeys" statements populated the "Username" text box, you might use the current "SendKeys" statement to populate the "Password" text box.

    • 8

      Write additional "SendKeys" statements to move to and populate the remaining controls you'd like to fill out. Run your program by pressing "F5." Your program will send the keystrokes to the Web page as though you were sending the keystrokes very quickly by hand.

Related Searches:

References

  • Mastering VBA for Office 2010 ; Richard Mansfield

Comments

Related Ads

Featured