在 Excel VBA 中将图像 (jpg) 转换为 base64?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert image (jpg) to base64 in Excel VBA?
提问by SpeeD72
I need to convert an image inside Excel(or through VBA) to base64(in the end I will make XML output).
我需要将 Excel 中的图像(或通过 VBA)转换为 base64(最后我将进行 XML 输出)。
How can I do this? Do I need to make a reference to DOM?
我怎样才能做到这一点?我需要引用 DOM 吗?
I′ve been reading this questionbut it only works for text strings not images...
我一直在阅读这个问题,但它只适用于文本字符串而不适用于图像......
Does anyone have any code that I can see?
有没有人有任何我可以看到的代码?
回答by michaelvdnest
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