2

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?

0m3r
  • 12,286
  • 15
  • 35
  • 71
Mike
  • 961
  • 6
  • 19
  • 43
  • How to create a file http://stackoverflow.com/questions/11503174/how-to-create-and-write-to-a-txt-file-using-vba. How to open an html http://stackoverflow.com/questions/3166265/open-an-html-page-in-default-browser-with-vba – Jules Feb 24 '16 at 00:39
  • 1
    HTML is just a plain text file: you can write one out using the built-in file tools in VBA or use the Scripting FileSystemObject. – Tim Williams Feb 24 '16 at 00:59

1 Answers1

4
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
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • 2
    Not sure what the `Private Declare Function` at the top or the `ShellExecute` on the bottom do, but the rest of it was very helpful for creating the file. I used 'FollowHyperlink HTMLFile' to open the file. – Mike Feb 25 '16 at 02:21