Convert image (jpg) to base64 in Excel VBA?

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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?

this question but it only works for text strings not images...

Does anyone have any code that I can see?

回答1:

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


回答2:

Please, take a look here: Base64 Encode & Decode Files

EDIT: Take a look on http://pastebin.com/f2a199d30



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!