VBA – HTML Encode and Decode String

So in my previous post

I demonstrated a couple techniques to encode/decode a URL/URI.

Today, I thought I’d demonstrate how easy it is to encode a text string into HTML with the proper HTML entities for special characters, and decode an HTML string back to plain text.

Encoding a Plain Text String into HTML

Encoding is very simple and you can use a function like:

'---------------------------------------------------------------------------------------
' Procedure : HF_HTMLEncode
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Take a plain text string and encode it as proper HTML using HTML Entities
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Early Binding -> Microsoft HTML Object Library
'             Late Binding  -> None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInput    : Plain text string to encode
'
' Usage:
' ~~~~~~
' ? HF_HTMLEncode("<pre>test</pre>")
'   Returns -> &lt;pre&gt;test&lt;/pre&gt;
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2022-12-13              Initial Public Release
'---------------------------------------------------------------------------------------
Public Function HF_HTMLEncode(sInput As String) As String
On Error GoTo Error_Handler
    #Const HF_EarlyBind = False    'True => Early Binding / False => Late Binding
                                   'Typically a Global Module Level Variable
    #If HF_EarlyBind = True Then
        Dim oHTMLFile              As MSHTML.HTMLDocument

        Set oHTMLFile = New MSHTML.HTMLDocument
    #Else
        Dim oHTMLFile              As Object

        Set oHTMLFile = CreateObject("HTMLFile")
    #End If

    oHTMLFile.Body.innerText = sInput
    HF_HTMLEncode = oHTMLFile.Body.innerHTML
 
Error_Handler_Exit:
    On Error Resume Next
    Set oHTMLFile = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: HF_HTMLEncode" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Then you can use it by doing something along the lines of:

? HF_HtmlEncode("<pre>test</pre>")

which will then return:

&lt;pre&gt;test&lt;/pre&gt;

 

Decoding an HTML String into Plain Text

Decoding is equally simple and you need only use a function like:

'---------------------------------------------------------------------------------------
' Procedure : HF_HTMLDecode
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Decode an HTML string into plain text by replaicing the HTML entities
'             with their corresponding caharacters
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Early Binding -> Microsoft HTML Object Library
'             Late Binding  -> None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInput    : HTML string to decode into plain text
'
' Usage:
' ~~~~~~
' ? HF_HTMLDecode("&lt;pre&gt;test&lt;/pre&gt;")
'   Returns -> <pre>test</pre>
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2022-12-13              Initial Public Release
'---------------------------------------------------------------------------------------
Public Function HF_HTMLDecode(sInput As String) As String
On Error GoTo Error_Handler
    #Const HF_EarlyBind = False     'True => Early Binding / False => Late Binding
                                    'Typically a Global Module Level Variable
    #If HF_EarlyBind = True Then
        Dim oHTMLFile              As MSHTML.HTMLDocument

        Set oHTMLFile = New MSHTML.HTMLDocument
    #Else
        Dim oHTMLFile              As Object

        Set oHTMLFile = CreateObject("HTMLFile")
    #End If

    oHTMLFile.Body.innerHTML = sInput
    HF_HTMLDecode = oHTMLFile.Body.innerText
 
Error_Handler_Exit:
    On Error Resume Next
    Set oHTMLFile = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: HF_HTMLDecode" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Then you can use it by doing something along the lines of:

? HF_HtmlDecode("&lt;pre&gt;test&lt;/pre&gt;")

which will then return:

<pre>test</pre>

 
These functions use basic VBA code, use no references (if using Late Binding) and thus are bitness independent (so it will run equally on 32 or 64-bit installations). If you want to use Early Binding the you will need to add a reference to the ‘Microsoft HTML Object Library‘.
 

Other Resources on the Subject