简体   繁体   English

如何使用VBA宏将背景颜色从HTML导入Excel

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

i try to get some information out of a table within a website by macro to an excel table. 我试图通过宏到Excel表从网站的表中获取一些信息。 usually I just use 通常我只是用

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

and paste it to any excel sheet which is enough for my needs. 并将其粘贴到任何足以满足我需求的Excel工作表中。 but the website was changed. 但网站已更改。 the problem is now, that I need to get the information of website cell with the following code: 现在的问题是,我需要使用以下代码来获取网站单元的信息:

<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

by that code I need to get the value for the background color (which can be #00dd00 or #000000 or #ff0000 or #0000ff) and paste it to excel cell B5. 通过该代码,我需要获取背景色的值(可以是#00dd00或#000000或#ff0000或#0000ff),并将其粘贴到excel单元格B5中。 then the macro need to get the background color value below the one before and paste it to the cell B6 (and so on and so on). 那么宏需要使背景色值低于之前的值并将其粘贴到单元格B6中(依此类推,以此类推)。

any idea how to realize that? 知道如何实现吗?

Here is my first Idea: I parse through the whole sourcecode, look for a certain string and copy the following charakters with that code: 这是我的第一个主意:我解析整个源代码,查找某个字符串,然后使用该代码复制以下字符:

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

That seems to work. 这似乎有效。 The positions are correct BUT the search result is only: VServer Usage" border="0", which means that the whole part 位置正确,但搜索结果仅为:VServer用法“ border =” 0“,表示整个部分

style="background-color:#00dd00"

is not found and ignored. 找不到并忽略。 I also tried it with: 我也尝试过:

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

but all with the same result :( 但都具有相同的结果:(

Something like (untested): 像(未经测试的):

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