使用VBA在IE中填写表单

时间:2014-03-19 20:35:19

标签: excel vba internet-explorer

希望在我的工作表上的特定单元格中填写IE中的表单;研究让我:

Function FillInternetForm()
  Dim ie As Object
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Navigate     "http://helppointinfo.farmersinsurance.com/OCR/Labor_Rates/laborrates.asp"
'go to web page listed inside quotes
  ie.Visible = True
  While ie.Busy
    DoEvents  'wait until IE is done loading page.
  Wend
  ie.Document.all("RatesForm").Value = Sheets("NAT").Range("C2").Value

End Function

修订版

<html>
<head>
    <meta name="GENERATOR" content="Microsoft Visual Studio 6.0" />
    <title>Labor Rates</title>
    <script language="javascript" type="text/javascript">
        //Function is called when Enter Zip text box is changed or when Drill down zip is selected
        function FindRates() {
            if (document.RatesForm.DirectZip.value + "Empty" != "Empty" && document.RatesForm.ZipDD.value + "Empty" != "Empty") {
                if (ValidateZip()) {
                    document.RatesForm.StateDD.value = ""
                    var strZipCode = document.RatesForm.ZipDD.value
                    document.getElementById("ErrorZip").style.display = "None"
                    document.getElementsByTagName("IFRAME").item("DisplayReport").src = "RatesHandler.asp?ZipCode=" + strZipCode
                }
                else {
                    document.getElementById("ErrorZip").style.display = "Block"
                }
            }
            else if (document.RatesForm.DirectZip.value + "Empty" != "Empty") {

                if (ValidateZip()) {
                    document.RatesForm.StateDD.value = ""
                    var strZipCode = document.RatesForm.DirectZip.value
                    document.getElementById("ErrorZip").style.display = "None"
                    document.RatesForm.submit();
                    document.getElementsByTagName("IFRAME").item("DisplayReport").src = "RatesHandler.asp?ZipCode=" + strZipCode
                }
                else {
                    document.getElementById("ErrorZip").style.display = "Block"
                }
            }
        }
        //Function called when State is selected from state Drop Down
        function StateRates() {
            var strState = document.RatesForm.StateDD.value
            document.getElementsByTagName("IFRAME").item("DisplayReport").src = "RatesHandler.asp?State=" + strState
            document.RatesForm.DirectZip.value = ""
            document.RatesForm.ZipDD.value = ""
            document.getElementById("ErrorZip").style.display = "None"
            document.getElementById("TabTag").style.display = "None"
        }
        //Function Disables the Drill Down Zip to stop a double function issue
        function disableDD() {
            document.RatesForm.ZipDD.disabled = true;
            document.getElementById("TabTag").style.display = "Block"
        }
        function ValidateZip() {
            var re = /^\d{5}([\-]\d{4})?$/;
            var testBool = re.test(document.RatesForm.DirectZip.value);
            return (testBool);
        }
        function disableEnterKey(e) {
            var key;
            if (window.event)
                key = window.event.keyCode;     //IE
            else
                key = e.which;     //firefox
            if (key == 13)
                return false;
            else
                return true;
        }


    </script>
    <link rel="stylesheet" type="text/css" href="css/main.css" />


  </head>

    <body>

<div class="DocumentTitle">Labor Rates Lookup</div>
<form name="RatesForm" method="post" >
    <table class="SearchTable" border="0" cellpadding="0" cellspacing="0">
        <tr class="SearchHeader">
            <td colspan="2" >By Zip</td>
        </tr>
        <tr class="SearchRow">
            <td>Enter Zip <br /><input class="SearchInput" id="DirectZip" name="DirectZip" value="" onkeypress="return disableEnterKey(event)" onblur="FindRates();" onfocus="disableDD();"  /></td>
            <td>
                <span style="display:block;">Drill Down Zip<br />

                    <select disabled = "disabled" id="Select1" name="ZipDD" class="SearchSelect" style="width:200px;"onchange="FindRates();">
                        <option class='SearchSelect' selected='selected'>Please Enter Zip</option>
                    <option class='SearchSelect' selected='selected'></option>
                    </select>
                </span>
            </td>

        </tr>
        <tr class="SearchRow" style="text-align:center"><td colspan="2"><span id="ErrorZip" style="display:none;color:Red;">Please Enter a Valid US Zip Code!</span></td></tr>
        <tr class="SearchRow" style="text-align:center"><td colspan="2"><span id="TabTag" style="display:none">Enter Zip Code and hit Tab.</span></td></tr>

        <tr class="SearchHeader">
            <td  colspan="2" >By State</td>
        </tr>
        <tr class="SearchRow">
            <td   colspan="2" style="text-align:center;" >
                <select id="ByState" name="StateDD" class="SearchSelect" onchange="StateRates();">
                    <option selected="selected"></option>

                        <option value = "AL" style='width:98%'>Alabama</option>

                        <option value = "AK" style='width:98%'>Alaska</option>

                        <option value = "AZ" style='width:98%'>Arizona</option>

                        <option value = "AR" style='width:98%'>Arkansas</option>

                        <option value = "CA" style='width:98%'>California</option>

                        <option value = "CO" style='width:98%'>Colorado</option>

                        <option value = "CT" style='width:98%'>Connecticut</option>

                        <option value = "DE" style='width:98%'>Delaware</option>

                        <option value = "DC" style='width:98%'>Dist. Columbia</option>

                        <option value = "FL" style='width:98%'>Florida</option>

                        <option value = "GA" style='width:98%'>Georgia</option>

                        <option value = "HI" style='width:98%'>Hawaii</option>

                        <option value = "ID" style='width:98%'>Idaho</option>

                        <option value = "IL" style='width:98%'>Illinois</option>

                        <option value = "IN" style='width:98%'>Indiana</option>

                        <option value = "IA" style='width:98%'>Iowa</option>

                        <option value = "KS" style='width:98%'>Kansas</option>

                        <option value = "KY" style='width:98%'>Kentucky</option>

                        <option value = "LA" style='width:98%'>Louisiana</option>

                        <option value = "ME" style='width:98%'>Maine</option>

                        <option value = "MD" style='width:98%'>Maryland</option>

                        <option value = "MA" style='width:98%'>Massachusetts</option>

                        <option value = "MI" style='width:98%'>Michigan</option>

                        <option value = "MN" style='width:98%'>Minnesota</option>

                        <option value = "MS" style='width:98%'>Mississippi</option>

                        <option value = "MO" style='width:98%'>Missouri</option>

                        <option value = "MT" style='width:98%'>Montana</option>

                        <option value = "NE" style='width:98%'>Nebraska</option>

                        <option value = "NV" style='width:98%'>Nevada</option>

                        <option value = "NH" style='width:98%'>New Hampshire</option>

                        <option value = "NJ" style='width:98%'>New Jersey</option>

                        <option value = "NM" style='width:98%'>New Mexico</option>

                        <option value = "NY" style='width:98%'>New York</option>

                        <option value = "NC" style='width:98%'>North Carolina</option>

                        <option value = "ND" style='width:98%'>North Dakota</option>

                        <option value = "OH" style='width:98%'>Ohio</option>

                        <option value = "OK" style='width:98%'>Oklahoma</option>

                        <option value = "OR" style='width:98%'>Oregon</option>

                        <option value = "PA" style='width:98%'>Pennsylvania</option>

                        <option value = "RI" style='width:98%'>Rhode Island</option>

                        <option value = "SC" style='width:98%'>South Carolina</option>

                        <option value = "SD" style='width:98%'>South Dakota</option>

                        <option value = "TN" style='width:98%'>Tennessee</option>

                        <option value = "TX" style='width:98%'>Texas</option>

                        <option value = "UN" style='width:98%'>Unknown</option>

                        <option value = "UT" style='width:98%'>Utah</option>

                        <option value = "VT" style='width:98%'>Vermont</option>

                        <option value = "VA" style='width:98%'>Virginia</option>

                        <option value = "WA" style='width:98%'>Washington</option>

                        <option value = "WV" style='width:98%'>West Virginia</option>

                        <option value = "WI" style='width:98%'>Wisconsin</option>

                        <option value = "WY" style='width:98%'>Wyoming</option>

                </select>
            </td>   
        </tr>
    </table>
</form>
<div>
    <iframe name="DisplayReport" id="DisplayReport" src="LaborCover.asp" frameborder="0"  style="LEFT: 0%; WIDTH: 75%; TOP: 0%; HEIGHT:550px; BACKGROUND-COLOR:#CCCCCC; border:solid 0px black;"></IFRAME>
</div>
</body>

为什么我一直在ie.document.all

上收到错误,我的头靠在墙上

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

CSS选择器:

Pi带评论中的答案,您还可以使用#DirectZip中的CSS selector。 读取为ID为DirectZip的元素。 #表示ID。


CSS查询:

CSS selector


VBA:

您使用文档的.querySelector方法来应用CSS选择器:

.document.querySelector("#DirectZip")