Last week I wrote a function which takes a file path and returns the base64 PNG code for that image. Since it isn’t straight-forward for everyone as to how to convert that JScript code to VBScript I decided to do just that:


Public Function convertImageToBase64(filePath)
  Dim inputStream
  Set inputStream = CreateObject("ADODB.Stream")
  inputStream.Open
  inputStream.Type = 1  ' adTypeBinary
  inputStream.LoadFromFile filePath
  Dim bytes: bytes = inputStream.Read
  Dim dom: Set dom = CreateObject("Microsoft.XMLDOM")
  Dim elem: Set elem = dom.createElement("tmp")
  elem.dataType = "bin.base64"
  elem.nodeTypedValue = bytes
  convertImageToBase64 = "data:image/png;base64," & Replace(elem.text, vbLf, "")
End Function

Feel free to use this code in your own projects. šŸ˜Ž

Categories: BlogVBAVBScript

3 Comments

avi · June 14, 2015 at 4:31 AM

very nice article!
very helpfull.
i used this web site for testing:
http://ebase64encode.org/

Don · June 22, 2016 at 4:28 PM

Can’t believe this worked. Excellent!

Zeeshan Bilal · February 23, 2017 at 8:28 AM

Dear Sir,

Thank you for your efforts, can you please ellaborate how can i use this.

I have a list of product and there images now i want to convert them on base64, now images are saved in Mac database or i can also paste them on excel cell please tell me how to utilize it.

Leave a Reply

Your email address will not be published. Required fields are marked *