English 中文(简体)
Convert image (jpg) to base64 in Excel VBA?
原标题:

I need to convert an image inside Excel (or through VBA) to base64 (in the end I will make XML output).

How can I do this? Do I need to make a reference to DOM?

I´ve been reading this question but it only works for text strings not images...

Does anyone have any code that I can see?

问题回答

Heres a function. Can t remember where I got it from.

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




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签