[英]VBA: excel data to html table entry
我正在處理一些VBA代碼,目的是從excel工作表中獲取信息並進入網頁中的表格。 到目前為止,我已經設法打開Internet Explorer,可以參考我的工作表。 我要插入數據的表的代碼如下所示:
<td valign="top" align="right" class="label">
Problem <br>Description
<font color="red" size="+1">*</font>
</td>
<td class="data" colspan="5">
<textarea name="eventVO.problemComments" cols="150" rows="3" id="eventVO.problemComments" class="data" title="Problem <br>Description">fffff</textarea>
</td>
</tr>
當我嘗試使用以下代碼修改“ textarea”時,收到無效的參數:
Sub FillInternetForm()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("aqe.xlsm")
Set ws = wb.Worksheets("sheet1")
'open xlm for reference
Dim IE As Object
Set IE = New InternetExplorerMedium
'create new instance of IE
'With IE
IE.Navigate "url"
'go to all quality events
IE.Visible = True
'Dim Document As HTMLDocument
'Set Document = .Document
Set elems = IE.Document.getElementsByTagName("tr")
'For Each e In elems
lookFor = "<td>"
For x = 2 To 10
If elems.innerText Like "eventVO.problemComments" Then
'finding correct <tr>
elems.getElementsByTagName("textarea").Value = ws.Cells("x, 9")
'enter problem description
'For x = 2 To 10
'get full html inside the <tr><tr>
'fullHTML = e.innerHTML
End If
如果使用elems.getElementsByTagName,則會收到運行時錯誤438。 如何克服此錯誤並將問題描述textarea的值設置為單元格值?
您有一個ID,因此使用該ID。 從理論上講,它應該是唯一的。
IE.Document.getElementById("eventVO.problemComments")
其他:
更多的HTML和/或URL將有助於進一步建議。
但是,如果您打算循環遍歷具有textarea
標簽的元素集合,則根據@TimWilliams注釋,您將遍歷該集合並按索引訪問單個項。 您可能需要修復分配語法,但是ws.Cells("x, 9")
將不起作用,並且我可能會使用CSS選擇器來獲取要遍歷的nodeList
。
CSS選擇器:
.document.querySelectorAll("textarea[name='eventVO.problemComments']")
這表示具有標簽名稱textarea
選擇元素的屬性name
值為'eventVO.problemComments'
。
對於循環返回的nodeList
您將具有以下內容:
Dim x As Long, aNodeList As Object
Set aNodeList =IE.document.querySelectorAll("textarea[name='eventVO.problemComments']")
For x = 0 To aNodeList.Length -1
aNodeList.Item(x) = ws.Cells(x+2,9) '<Note how there is no "" inside the ()
' aNodeList(x) '<==potentially variation in syntax
Next x
您也許還可以循環測試該名稱的textarea類對象的集合:
Dim a As Object, b As Object
Set a = ie.document.getElementsByTagName("textarea") '<=collection
For Each b In a
'On Error Resume Next ''<=This may potentially be needed if not all have a name attribute
If InStr(1, b.getAttribute("name").innerText, "eventVO.problemComments") > 0 Then 'You might be able to just say If b.Name = eventVO.problemComments
'Do something with the matched element
End If
'On Error GoTo 0
Next b
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.