'Open HTML file in excel, preserving HTML inline styling
I'm trying to open a HTML file in excel (from VBA) so that the HTML inline styling is preserved. Are there any special ways of doing this because currently I've tried creating the HTML as a string and inserting into a cell using ActiveSheet.Range("A10") = HTMLStr but this isn't working and also when I open the file in Excel using:
Workbooks.Open
this also does not render the HTML correctly
(edit my final result is to turn the html into a pdf)
Solution 1:[1]
Why do you need to open the page in Excel - is it just so you can make a PDF (Excel 2007/10) ? You could try passing the HTML to IE and automating a copy/paste from there.
As an example, the following code will take a selection of cells with HTML, transfer each one to IE, then copy back the result.
Sub FormatHtmlViaIE()
Dim ie As Object, tr, c As Range
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.Navigate "about:blank"
Do While .busy
Loop
For Each c In Selection
.document.body.innerHTML = c.Value
.document.body.createtextrange.execCommand "Copy"
Selection.Parent.Paste Destination:=c
Next c
.Quit
End With
End Sub
Solution 2:[2]
Otherwise is open page in excel.
For Example
Sub OpenPage()
'officevb.com
Dim wb As Workbook
Set wb = Workbooks.Open("http://www.stackoverflow.com")
wb.SaveAs "c:\Your\Path\Here\NameOfFile", xlWorkbookDefault
End Sub
[]'s
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Williams |
Solution 2 | Bruno Leite |