vba · DOCS

Encoding to Base64

Maintained on

Base64 is an encoding scheme that represents data using 64 characters. It is commonly used in email, software, and web APIs.

This page shows how to convert text data to Base64 using VBA. The code uses late binding via CreateObject, so no reference settings are required.

Caution

The code relies on ADODB.Stream and MSXML, which are Windows COM components. It does not work on Excel for Mac.

Caution

This sample converts text with us-ascii, so it only supports ASCII strings such as alphanumeric characters. Multibyte characters (e.g. Japanese) are turned into ? before encoding. For such text, see “Handling non-ASCII text (UTF-8)” below.

Source Code

'*-------------------------------------------------------------
'* Encode text to Base64
'*
'* @param text The value to be converted
'* @return Base64 formatted data
'*-------------------------------------------------------------
Public Function EncodeToBase64(ByRef text As String) As String

  ' Prepare the object
  Dim node As Object
  Set node = CreateObject("Msxml2.DOMDocument.3.0").createElement("base64")

  ' Encode
  node.DataType = "bin.base64"
  node.nodeTypedValue = ConvertToBinary(text)

  ' Remove line breaks that cannot be removed by the function and return
  EncodeToBase64 = Replace(node.text, vbLf, "")
End Function

'*-------------------------------------------------------------
'* Convert string to binary data
'*
'* @param text The value to be converted
'* @return Binary data
'*-------------------------------------------------------------
Public Function ConvertToBinary(ByRef text As String)

  ' Prepare the object
  Dim BinaryStream As Object
  Set BinaryStream = CreateObject("ADODB.Stream")

  ' Configure the Stream
  With BinaryStream
    .Type = 2
    .Charset = "us-ascii"
    .Open
    .WriteText text
    .Position = 0
    .Type = 1
    .Position = 0
  End With

  ConvertToBinary = BinaryStream.Read
End Function

Test Execution

Execution

Public Sub execute()
    Debug.Print (EncodeToBase64("aaa"))
    Debug.Print (EncodeToBase64("aab"))
    Debug.Print (EncodeToBase64("aac"))
End Sub

Result

YWFh
YWFi
YWFj

Handling non-ASCII text (UTF-8)

Most web APIs expect the Base64 of a UTF-8 byte sequence. Simply changing the charset to utf-8 prepends a 3-byte BOM, so the result will not match other systems. After switching the stream to binary, set Position = 3 to skip the BOM.

'*-------------------------------------------------------------
'* Convert string to UTF-8 (without BOM) binary data
'*
'* @param text The value to be converted
'* @return Binary data
'*-------------------------------------------------------------
Public Function ConvertToBinaryUtf8(ByRef text As String)

  If Len(text) = 0 Then Exit Function

  Dim BinaryStream As Object
  Set BinaryStream = CreateObject("ADODB.Stream")

  With BinaryStream
    .Type = 2
    .Charset = "utf-8"
    .Open
    .WriteText text
    .Position = 0
    .Type = 1
    ' Skip the BOM (3 bytes)
    .Position = 3
  End With

  ConvertToBinaryUtf8 = BinaryStream.Read
End Function

Replace ConvertToBinary(text) in EncodeToBase64 with ConvertToBinaryUtf8(text), and "あ" becomes 44GC (the Base64 of the UTF-8 bytes E3 81 82).

Converting to Base64URL

Base64URL, used in JWTs and URLs, differs from standard Base64 as follows:

  • Replace + with - and / with _
  • Omit the trailing padding =
Public Function ToBase64Url(ByVal base64 As String) As String
  base64 = Replace(base64, "+", "-")
  base64 = Replace(base64, "/", "_")
  ToBase64Url = Replace(base64, "=", "")
End Function

How it works and caveats

  • Base64 splits 3 bytes (24 bits) into four 6-bit groups, so the data grows by about 4/3.
  • If the byte count is not a multiple of 3, the end is padded with = (e.g. "a" → YQ==).
  • Base64 is an encoding, not encryption. Encoding a password in Base64 does not protect it.
  • The Base64 output from MSXML contains a line break every 76 characters, which is why the sample removes vbLf.

Additional Information about Base64

Base64 is an encoding scheme that allows handling multibyte characters and binary data in communication environments where only 64 printable alphanumeric characters can be used and other characters cannot be handled. It is defined by MIME and widely used in email, which can only handle 7-bit data. Specifically, it uses 62 characters from A-Z, a-z, 0-9, two symbols (+, /), and the symbol ”=” is used for padding (to fill the remaining part).

Base64 - Wikipedia

As mentioned above, Base64 is still widely used today, and there are also other encoding schemes such as Base16 and Base32.

Since it is defined by MIME, it is used in VBA when the MIME type of the API specifies Base64, for example.

  • #Excel
  • #VBA