vba 在 Excel 中打开 HTML 文件,保留 HTML 内联样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7488288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:04:46  来源:igfitidea点击:

Open HTML file in excel, preserving HTML inline styling

excelvba

提问by dave

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:

我正在尝试在 excel(来自 VBA)中打开一个 HTML 文件,以便保留 HTML 内联样式。是否有任何特殊的方法可以做到这一点,因为目前我已经尝试将 HTML 创建为字符串并使用 ActiveSheet.Range("A10") = HTMLStr 插入到单元格中,但这不起作用,并且当我在Excel 使用:

Workbooks.Open

工作簿.打开

this also does not render the HTML correctly

这也不能正确呈现 HTML

(edit my final result is to turn the html into a pdf)

(编辑我的最终结果是将html变成pdf)

回答by Tim Williams

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.

为什么需要在 Excel 中打开页面 - 是否只是为了制作 PDF (Excel 2007/10)?您可以尝试将 HTML 传递给 IE 并从那里自动复制/粘贴。

As an example, the following code will take a selection of cells with HTML, transfer each one to IE, then copy back the result.

例如,下面的代码将选择带有 HTML 的单元格,将每个单元格传输到 IE,然后将结果复制回来。

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

回答by Bruno Leite

Otherwise is open page in excel.

否则是在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

[] 的