我可以使用VBA创建和打开HTML文件吗?

时间:2016-02-24 00:04:15

标签: html vba

我有一个生成我想要的HTML,CSS和JS的函数。所有代码都存储在字符串变量中。

如何让VBA创建.html文件并在默认的Web浏览器中打开它?

1 个答案:

答案 0 :(得分:1)

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
     (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal _
     lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub HTML()
'   // Define your variables.
    Dim HTMLFile As String

    HTMLFile = ActiveWorkbook.Path & "\test.html"
    Close

'   // Open up the temp HTML file and format the header.
    Open HTMLFile For Output As #1
        Print #1, "<html>"
        Print #1, "<head>"
        Print #1, "<style type=""text/css"">"
        Print #1, "  body { font-size:12px;font-family:tahoma } "
        Print #1, "</style>"
        Print #1, "</head>"
        Print #1, "<body>"

        Print #1, "<h2> HELLO VBA HTML </h2>"

        Print #1, "</body>"
        Print #1, "</html>"
    Close

    ShellExecute 0&, vbNullString, HTMLFile, vbNullString, _
      vbNullString, SW_SHOWNORMAL

End Sub