Encode/Decode using FileSystemObject and ADO Stream

Encode/Decode using FileSystemObject and ADO Stream

Encode/Decode using FileSystemObject and ADO Stream


'Encode/Decode using FileSystemObject and ADO Stream

Private PathToFile As String
Dim oStream ' objet stream
Dim oFso ' objet filesystem


Private Sub cmdEncode_Click()
    Dim imgStrm
    Dim f
    TextBox1.Text = ""
    TextBox1.Text = BinToB64(PathToFile & "ic_hand_black_36dp.png", oStream)
    imgStrm = TextBox1.Text
    'Write to file
    'Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Set f = oFso.OpenTextFile(PathToFile & "hand.txt", 8, True) '"true=create if not exist)
    f.Write CStr(imgStrm)
    f.Close
    
    If Not oFso.FileExists(PathToFile & "hand.png") Then 'oFso.DeleteFile "c:\somefile.txt"
        Set oStream = oFso.CreateTextFile(PathToFile & "hand.png", True)
        B64ToBin imgStrm, oStream, 0 '0 - FileSystem 1 - ADO
        oStream.Close

    End If


End Sub

Private Sub UserForm_Initialize()

    PathToFile = ThisWorkbook.Path & "\"

    Set oStream = CreateObject("ADODB.Stream")

    If Not oStream Is Nothing Then
        'pas bon, on teste ensuite le filesystem
        Set oFso = CreateObject("Scripting.FileSystemObject")
    Else
        MsgBox "Unable to initialize stream."
    End If
    
End Sub


Share This