如何提取 HTML 源代码到 Excel VBA

时间:2021-01-21 17:06:41

标签: html excel vba insert

I',尝试将整个 HTML 正文复制并粘贴到 Excel 工作表中。现在我有这个

Sub audycje()

Dim strona As Object
Dim adres As String
Dim wb As Workbook
Dim a As String

Set strona = CreateObject("InternetExplorer.Application")
Set wb = ThisWorkbook

adres = InputBox("Podaj adres strony")

strona.navigate (adres)
wb.Worksheets("Dane").Range("B2") = strona.body.innerHTML
strona.Quit
    End Sub 

只是无法将 HTML 插入到 excel 中;/

[编辑] 我得到了这个,它工作正常,但是...

Sub audycje()

Dim strona As Object
Dim adres As String
Dim wb As Workbook
Dim a As Object

Set strona = CreateObject("InternetExplorer.Application")
Set wb = ThisWorkbook
adres = InputBox("Podaj adres strony")
If adres = "" Then
MsgBox ("Nie podano strony do załadowania")
Exit Sub
End If
strona.Visible = True
strona.navigate (adres)
wb.Worksheets("Dane").Range("B2") = strona.document.body.innerHTML
    End Sub

整个 HTML 正文都在一个单元格中。如何传播?

1 个答案:

答案 0 :(得分:0)

Did you mean something like this?

    Sub audycje()
    
    Dim strona As Object
    Dim adres As String
    Dim wb As Workbook
    Dim a As Object
    Dim str_var As Variant
    
    Set strona = CreateObject("InternetExplorer.Application")
    Set wb = ThisWorkbook
    adres = InputBox("Podaj adres strony")
    If adres = "" Then
       MsgBox ("Nie podano strony do zaladowania")
    Exit Sub
    End If
    
    Set strona = CreateObject("htmlfile")   'Create HTMLFile Object
    With CreateObject("msxml2.xmlhttp")  'Get the WebPage Content
       .Open "GET", adres, False
       .send
       strona.Body.Innerhtml = .responseText
    End With
    
    'Split_with_delimiter_newline
    split_var = Split(strona.Body.Innerhtml, Chr(10))
    
    Application.ScreenUpdating = False
    
    For i = 0 To UBound(split_var, 1)
       Cells(2 + i, 2).Value2 = split_var(i)
    Next i
    
    Application.ScreenUpdating = True
    
    End Sub