Excel将值传递到文本字段

时间:2018-03-24 11:22:40

标签: excel vba excel-vba

我创建了以下代码,转到我想要的网站,从图1中的范围A传递一个值,在文本字段中输入,然后点击查找按钮,我已到达我可以从中选择的位置下拉菜单,但我无法将参数传递给文本字段,并收到错误“对象变量或未设置块变量”

Sub Test()

Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))

For Each cell In rng

    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("home URL")
    Do
        If ie.ReadyState = 4 Then
            ie.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop

    ie.Document.forms(0).all("txtUsername").Value = ""
    ie.Document.forms(0).all("txtPassword").Value = ""
    ie.Document.forms(0).submit
    ie.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))

        ie.Visible = True
        ie.Navigate "Search URL"

    Application.Wait (Now + TimeValue("00:00:02"))

        For Each Post In ie.Document.getElementsByName("cboFieldName")(0).getElementsByTagName("option")
            If InStr(Post.innerText, "Global Service Reference") > 0 Then Post.Selected = True: Exit For
        Next Post

 Application.Wait (Now + TimeValue("00:00:02"))

    ie.Document.getElementsByName("textfieldvalue")(0).Select

    ie.SendKeys (cell.Value)

    DoEvents

    ie.Document.getElementsByName("cmdfind")(0).Click

Next cell


End Sub

以下是该网站的代码。

<form action="search.asp?Find=1" method="post" onsubmit="return validate(this);">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
    <tbody><tr>
        <td valign="top">
            <br><table border="0" cellspacing="0" cellpadding="2" width="100%">
                <tbody><tr>
                    <td colspan="3"><b><font size="3" face="cambria" color="#e60000">Please enter your search criteria:</font></b></td>
                </tr>
                <tr>
                    <td width="20%">
                        <select name="cboFieldName">
                            <option value="0"></option>
                            <option value="1">Customer Name</option>
                            <option value="2">Customer Reference</option>
                            <option value="3">Site Name</option>
                            <option value="4">Site City</option>
                            <option value="5">Site Country</option>
                            <option value="6">Global Service Reference</option>
                            <option value="7">Customer Service Reference</option>
                        </select>
                    </td>
                    <td align="center" width="10%"><font size="2" face="Tahoma" color="#000000">contains</font></td>
                    <td width="50%">
                        <input type="text" name="txtFieldValue" value="">
                    </td>
                </tr>
            </tbody></table>
        </td>
        <td width="25%" valign="top">
            <table border="0" cellpadding="0" cellspacing="0">
                <tbody><tr>
                    <td width="2" bgcolor="#003399">&nbsp;</td>
                    <td width="10">&nbsp;</td>
                    <td>
                        <font size="2" face="Tahoma">
                        <input type="submit" style="background-color:#a3418f" name="cmdFind" value="Find">
                        <p>
                        <a href="login/welcome.asp" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">All Customers</a><br><br>
                        <a href="search.asp" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">New Search</a><br><br>
                        <a href="javascript:ShowHelp()" target="_self" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">Help...</a>
                    </p></font></td>
                </tr>
            </tbody></table>
        </td>
    </tr>
</tbody></table>
</form>

1 个答案:

答案 0 :(得分:0)

Sub Test()

Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))
Dim ie As Object

For Each cell In rng

    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("https://gcd.ad.plc.cwintra.com/GCD_live/login/login.asp")
    Do
        If ie.ReadyState = 4 Then
            ie.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop

    ie.Document.forms(0).all("txtUsername").Value = "s"
    ie.Document.forms(0).all("txtPassword").Value = ""
    ie.Document.forms(0).submit
    ie.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))

    With ie
        .Visible = True
        .Navigate "https://gcd.ad.plc.cwintra.com/GCD_live/search.asp"
        While .Busy = True Or .ReadyState < 4: DoEvents: Wend

        For Each Post In ie.Document.getElementsByName("cboFieldName")(0).getElementsByTagName("option")
            If InStr(Post.innerText, "Global Service Reference") > 0 Then Post.Selected = True:

            ie.Document.forms(0).all("txtFieldValue").Value = cell.Value

            ie.Document.forms(0).submit


        Next Post

        DoEvents

    End With


    Application.Wait (Now + TimeValue("00:00:02"))


Next cell


End Sub