Private Sub DecodeBase64()
' This script reads base64 encoded picture from file named B64picture.txt,
' converts it in to back to binary reprisentation using encoding abilities
' of MSXml2.DOMDocument object and saves data to test.jpg file
Const foForReading = 1 ' Open base 64 code file for reading
Const foAsASCII = 0 ' Open base 64 code file as ASCII file
Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
Const adTypeBinary = 1 ' Binary file is encoded
Dim objFSO, objFileIn, objStreamIn, objXML, objDocElem, objStream
' Open data stream from base64 code file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Sheets("Feuil2").Select
NB_Ligne = Range("A1000").End(xlUp).Row
Dim r As Integer
Dim NomSource As String
Dim NomCible As String
'
For r = 1 To NB_Ligne
NomSource = CheminXml & Cells(r, 20).Value & ".txt"
NomCible = CheminXml & Cells(r, 20).Value & ".jpg"
Set objFileIn = objFSO.GetFile(NomSource)
Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
With objDocElem
.DataType = "bin.base64"
.Text = objStreamIn.ReadAll()
End With
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = adTypeBinary
.Open
End With
With objStream
.Write objDocElem.nodeTypedValue
.SaveToFile NomCible, adSaveCreateOverWrite
End With
Next
Set objFSO = Nothing: Set objFileIn = Nothing: Set objStreamIn = Nothing
Set objXML = Nothing: Set objDocElem = Nothing: Set objStream = Nothing
End Sub