Skip to content

Instantly share code, notes, and snippets.

@cainhill
Last active May 26, 2021 11:50
Show Gist options
  • Save cainhill/ad4457dcd16e13f8859a to your computer and use it in GitHub Desktop.
Save cainhill/ad4457dcd16e13f8859a to your computer and use it in GitHub Desktop.
' http://web.archive.org/web/20060527094535/http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp
' http://cwestblog.com/2013/09/23/vbscript-convert-image-to-base-64/
Public Function ConvertFileToBase64(strFilePath As String) As String
Const UseBinaryStreamType = 1
Dim streamInput: Set streamInput = CreateObject("ADODB.Stream")
Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlElem: Set xmlElem = xmlDoc.createElement("tmp")
streamInput.Open
streamInput.Type = UseBinaryStreamType
streamInput.LoadFromFile strFilePath
xmlElem.dataType = "bin.base64"
xmlElem.nodeTypedValue = streamInput.Read
ConvertFileToBase64 = Replace(xmlElem.Text, vbLf, "")
Set streamInput = Nothing
Set xmlDoc = Nothing
Set xmlElem = Nothing
End Function
Public Sub ConvertBase64ToFile(strFilePath As String, strBase64 As String)
Const UseBinaryStreamType = 1
Const SaveWillCreateOrOverwrite = 2
Dim streamOutput: Set streamOutput = CreateObject("ADODB.Stream")
Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlElem: Set xmlElem = xmlDoc.createElement("tmp")
xmlElem.dataType = "bin.base64"
xmlElem.Text = strBase64
streamOutput.Open
streamOutput.Type = UseBinaryStreamType
streamOutput.Write = xmlElem.nodeTypedValue
streamOutput.SaveToFile strFilePath, SaveWillCreateOrOverwrite
Set streamOutput = Nothing
Set xmlDoc = Nothing
Set xmlElem = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment