使用Excel VBA填写网站搜索栏

时间:2012-08-02 20:10:03

标签: html forms excel vba excel-vba

我正在尝试创建一个Excel宏,将数据插入到Web搜索表单中,然后将结果复制到表中。 Web搜索表单实际上不是一个“表单”,而是一个空白表,所以我不能只更改表单的输入值,因为没有:

<td valign="top">
    <table border="0" cellspacing="0" cellpadding="2">
        <tr>
            <th class="navLabelText" align="left">Order:</th>
            <td>
                <input class="navEditField" id="opt_ordernumber_int" name="ordernumber" type="text" size="6" maxlength="6" />
            </td>
        </tr>
    </table>
</td>
<td width="10">&nbsp;</td>

HTML继续使用更多相同类型的表单(我猜是用Java编码,因为该站点是.jsp)。有什么方法可以将值传递到空白表中吗?

这是我到目前为止所拥有的:

Sub featurecode()

Dim ie As Object
Dim doc As HTMLDocument
Dim links As IHTMLElementCollection
Dim link As HTMLAnchorElement
Dim i As Integer
Dim found As Boolean
Dim todaysURL As String

Dim objElement As Object
Dim objCollection As Object

Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = True 'false
ie.navigate "https://website.com"
Application.StatusBar = "Loading Feature Codes"

Do Until ie.readyState = IE_READYSTATE.complete: DoEvents: Loop

Set doc = ie.document


' Find the input tag of the order form and submit button:
Set objCollection = ie.document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).Name = "ordernumber" Then

        ' Set text for search
        objCollection(i).Value = "655032"

    Else
        If objCollection(i).Type = "submit" And objCollection(i).Name = "Submit" Then

            ' "Search" button is found
            Set objElement = objCollection(i)
            objElement.Click

        End If
    End If
    i = i + 1
Wend

End Sub

我遇到麻烦的部分是:

If objCollection(i).Name = "ordernumber" Then

    ' Set text for search
    objCollection(i).Value = "655032"

通常你可以改变表单的HTML值,但在这种情况下输入标签中没有HTML值,所以我很茫然。我的目标是简单地在表单中插入订单号并点击提交按钮。很遗憾,我无法向您展示该网站,因为它是一个内部公司网站,但这里是相关信息的屏幕截图:screenshot

谢谢!

1 个答案:

答案 0 :(得分:-1)

我发现VBA中有一些元素,包括INPUT,你必须首先关注元素:

    objCollection(i).Focus
    objCollection(i).Value = "655032"