Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

How to extract HTML source code to excel VBA

I', trying to copy and paste the whole HTML body to excel worksheet. For now i have this

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 

Just cant get the HTML to be inserted into excel ;/

[EDIT] I got this and it's working ok but...

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

The whole HTML body is in one cell. How to spread it ?

question from:https://stackoverflow.com/questions/65832333/how-to-extract-html-source-code-to-excel-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...