Mike
Mike

Reputation: 1011

Can I create and open an HTML file with VBA?

I have a function that generates the HTML, CSS, and JS that I want. All the code is stored in a string variable.

How would I get VBA to create a .html file and open it in the default web browser?

Upvotes: 2

Views: 8182

Answers (1)

0m3r
0m3r

Reputation: 12499

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

Upvotes: 5

Related Questions