How to convert an attached image to Base64 format.

Solved/Closed
Raj_1562 Posts 29 Registration date Wednesday August 26, 2020 Status Member Last seen August 29, 2022 - Jan 18, 2021 at 09:34 AM
 Raj - Jan 28, 2021 at 10:46 PM
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
Related:

1 response

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Jan 28, 2021 at 12:18 PM
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

1
Thanks, Trowa for all your analysis on this.

Appreciated.

Regards,
Raj
0