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?

2

1 Answer 1

5
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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.