1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| Sub InsertImgFromBase64(sheet As String, address As String, varImgName As String, varBase64 As String) Dim tempFile As String 'Save base64 string to temp folder tempFile = Environ("temp") & "\" & varImgName 'save byte array to temp file Open tempFile For Binary As #1 Put #1, 1, DecodeBase64(varBase64) Close #1
'insert image from temp file Sheets(sheet).Range(address).Select Sheets(sheet).Pictures.Insert tempFile
'kill temp file Kill tempFile End Sub
Private Function DecodeBase64(ByVal strData As String) As Byte()
Dim objXML As Object 'MSXML2.DOMDocument Dim objNode As Object 'MSXML2.IXMLDOMElement
'get dom document Set objXML = CreateObject("MSXML2.DOMDocument")
'create node with type of base 64 and decode Set objNode = objXML.createElement("b64") objNode.DataType = "bin.base64" objNode.Text = strData DecodeBase64 = objNode.nodeTypedValue
'clean up Set objNode = Nothing Set objXML = Nothing
End Function
Sub InsertImgFromBase64Test() InsertImgFromBase64 "Sheet1", "E15", "test2.png", Range("A1") End Sub
|