How to convert an attached image to Base64 format. [Solved]

Report
Posts
22
Registration date
Wednesday August 26, 2020
Status
Member
Last seen
January 18, 2021
-
 Raj -
Hi All,

Hope you are doing good!

I would like to convert the attached image into Base64 format in excel via VBA. Is it possible to change the image file into Base64 format the next cell?



Thanks in Advance

Regards,
Raj

1 reply

Posts
2696
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
January 28, 2021
457
Hi Raj,

I'm not familiar with Base64. I did find this code on the world wide web:
Public Function EncodeFile(strPicPath As String) As String
    Const adTypeBinary = 1          ' Binary file is encoded

    ' Variables for encoding
    Dim objXML
    Dim objDocElem

    ' Variable for reading binary picture
    Dim objStream

    ' Open data stream from picture
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath)

    ' Create XML Document object and root node
    ' that will contain the data
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.dataType = "bin.base64"

    ' Set binary value
    objDocElem.nodeTypedValue = objStream.Read()

    ' Get base64 value
    EncodeFile = objDocElem.Text

    ' Clean all
    Set objXML = Nothing
    Set objDocElem = Nothing
    Set objStream = Nothing

End Function


Place the code in a standard module. In excel use it as any other function: =EncodeFile(cell reference with the entire path of the picture)


Best regards,
Trowa

Thanks, Trowa for all your analysis on this.

Appreciated.

Regards,
Raj

Subscribe To Our Newsletter!

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!