2012-08-02 149 views
0

我試圖製作一個Excel宏,它將數據插入到網頁搜索表單中,然後將結果複製到表中。網絡搜索形式並不是一個真正的「形式」,而是一個空白表,所以我不能只是改變形式的輸入值,因爲也沒有了:使用Excel VBA填寫網站搜索欄

<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

謝謝!

+3

歡迎StackOverflow上。你到目前爲止嘗試過什麼嗎? (作爲新用戶,您可能需要查看[問]常見問題解答,以獲取您需要的答案。) – Gaffi 2012-08-02 21:15:49

+0

我們能否查看該網站的鏈接? – 2012-08-02 23:58:31

+0

你能解釋你的目標嗎? – JimmyPena 2012-08-03 00:13:59

回答

-1

我VBA中的一些元素,包括輸入發現,你必須專注元素先上:

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