簡體   English   中英

如何使用VBA宏將背景顏色從HTML導入Excel

[英]How to import background color from HTML to Excel with VBA Macro

我試圖通過宏到Excel表從網站的表中獲取一些信息。 通常我只是用

ie2.ExecWB 17, 0 '// SelectAll
ie2.ExecWB 12, 2 '// Copy selection

並將其粘貼到任何足以滿足我需求的Excel工作表中。 但網站已更改。 現在的問題是,我需要使用以下代碼來獲取網站單元的信息:

<tr class="odd"><td><a href="xxxxxxx">
<img border="0" src="letter.png" title="Titel0" /></a><td></td>
<td><img title="VServer Usage" style="background-color:#00dd00" border="0" src="/ce.png" />&nbsp;<a   
href="testtesttest">blablabla</a></td>
<td><a href="http://maps.google.de/" target="_blank">TEST TEST</a></td>
<td><img border="0" src="/damage.png" title="Titel1"/></a></td>
<td></td><td><img border="0" src="/card.png" title="Titel2"/></a></td>
<td></td><td><img border="0" src="/key.png" title="Titel3" /></a></td>
<td><img border="0" src="/immo.png" title="Titel4" /></a></td>
<td><img border="0" src="/locked.png" title="Titel5" /></a></td><td>101</td>
<td>102</td><td>103</td><td>104</td><td>105</td><td>106</td><td>107</td><td>Name</td>
</tr>

// and then it starts with the next line which has the same structure

通過該代碼,我需要獲取背景色的值(可以是#00dd00或#000000或#ff0000或#0000ff),並將其粘貼到excel單元格B5中。 那么宏需要使背景色值低於之前的值並將其粘貼到單元格B6中(依此類推,以此類推)。

知道如何實現嗎?

這是我的第一個主意:我解析整個源代碼,查找某個字符串,然后使用該代碼復制以下字符:

Dim strCountBody As String
Dim lStartPos As Long
Dim lEndPos As Long
Dim TextIWant As String
Dim Textpos As Integer
Dim searchchar As String

searchchar = "VServer Usage" 


Application.ScreenUpdating = False


Set WebBrowser1 = CreateObject("InternetExplorer.Application")
WebBrowser1.Visible = True ' zum testen anzeigen
WebBrowser1.Navigate "wwww.www..www"
While WebBrowser1.readyState <> 4
    'Warten, bis Seite geladen ist
    DoEvents
Wend

strCountBody = WebBrowser1.Document.body.innerHTML
textpos = InStr(5500, strCountBody, searchchar, vbTextCompare)

TextIWant = Mid(strCountBody, Textpos, 25)

Worksheets("Tabelle1").Range("J1").Value = TextIWant
Worksheets("Tabelle1").Range("K1").Value = Textpos

這似乎有效。 位置正確,但搜索結果僅為:VServer用法“ border =” 0“,表示整個部分

style="background-color:#00dd00"

找不到並忽略。 我也嘗試過:

WebBrowser1.Document.body.innerHTML
WebBrowser1.Document.body.outerHTML
WebBrowser1.Document.body.innerText
WebBrowser1.Document.body.outerText

但都具有相同的結果:(

像(未經測試的):

Dim tbl, rw

For Each tbl in ie.document.getElementsByTagName("table")
    For Each rw in tbl.body.rows

       debug.print rw.cells(2).getElementsByTagName("img")(1).style.backgroundColor

    Next rw
Next tbl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM