2012-11-07 31 views
1

我使用Access VBA打開Internet Explorer並在網站上填寫表單。 這個表格有自動編號的id,每隔一段時間就會改變一次,但是下面的「_id」總是相同的,我不確定是否或者怎樣使用「_id」將值插入該字段使用VBA IE自動選擇文本框

這是我到目前爲止,感謝蒂姆 林不知道這個網站究竟是如何工作的我在這裏相當新的,但是這是我到目前爲止,一個在單選按鈕上的最後一個問題

Dim ie As InternetExplorer 
Dim url As String 
Dim htmlDoc As MSHTML.HTMLDocument ' html object lib 
Dim htmlInputElemen As MSHTML.HTMLInputElement 
Dim htmlElementCol As MSHTML.IHTMLElementCollection 

url = www.abc.com 
ie.navigate url 
‘ this is for text box 
Set htmlDoc = ie.Document 
Set htmlElementCol = htmlDoc.getElementsByTagName("INPUT") 
    For Each htmlInputElemen In htmlElementCol 
     If htmlInputElemen.getAttribute("title") = "myTitle" Then 
      htmlInputElemen.Value = 「myTitle」 
'  ElseIf htmlInputElemen.getAttribute("title") = "myTitle2" Then 
      htmlInputElemen.Value = 「myTitle2」 
     End If 
    Next htmlInputElemen 
‘ this is for combo box 
Set htmlElementCol = htmlDoc.getElementsByTagName("select") 
Dim htmlSelectElem As MSHTML.HTMLSelectElement 
    For Each htmlSelectElem In htmlElementCol 
     If htmlSelectElem.getAttribute("title") = "myComboBox" Then 
      htmlSelectElem.Value = 「myComboBoxValue」 
     End If 
    Next htmlSelectElem 

‘now I have radio buttons which im not sure how to click using title, any ideas? 
I tried this but no luck 
Set htmlElementCol = htmlDoc.getElementsByTagName("span") 
Dim htmlSpanElem As MSHTML.HTMLSpanElement 
    For Each htmlSpanElem In htmlElementCol 
     If htmlSpanElem.getAttribute("title") = 「ABC」 Then 
      htmlSpanElem.Click 
     End If 
    Next htmlSpanElem 

<span class="ms-RadioText" title="ABC"> 
<input id="234sd87s89df"> 
<label for="234sd87s89df ">ABC</label> 

回答

1
Dim inputs, el 

Set inputs = document.getElementsByTagName("input") 

For Each el In inputs 
    If el.getAttribute("_id") = "R_Destination" Then 
     el.Value = "myValue" 
     Exit For 
    End If 
Next el 

編輯:這裏是與功能重構相同的代碼

Function GetElement(doc as object, tagName As String, attrName As String, _ 
             attrValue As String) As Object 
    Dim rv As Object 
    Dim elements, element 

    Set inputs = doc.getElementsByTagName(tagName) 

    For Each element In elements 
     If element.getAttribute(attrName) = attrValue Then 
      Set rv = element 
      Exit For 
     End If 
    Next element 

    Set GetElement = rv 

End Function 

用法:

Dim el 
Set el = GetElement(document, "input", "_id", "R_Destination") 
if not el is nothing then el.value = "myValue" 
+0

的偉大工程,非常感謝! – live2ride

+0

請提供更多關於「多個字段」 - 不同類型標籤的含義的更多信息?不同的'ID',但仍然輸入標籤?別的東西? –