Base64をデコード

Base64をデコードする。

VBAでBase64の文字列(バイナリ)のデータを元の文字列にデコードします。
デコードには「Msxml2.DOMDocument.3.0」と「ADODB.Stream」のオブジェクトを利用してデコードしています。

動作確認
Windows10/Excel2016:○
Windows11/Excel2024:○

エンコードの方法はこちらのサイトに掲載しています。
 「Base64にエンコードする」 

'******************************************************************************
' Base64をデコード
'******************************************************************************
Sub Main_Base64ToStirng()
    
    Dim sDecode As String
    sDecode = "77u/5ZC+6Lyp44Gv54yr44Gn44GC44KL"
    
    Debug.Print "(デコード前)"
    Debug.Print sDecode
    Debug.Print
    Debug.Print "(デコード後)"
    Debug.Print decodeToBase64(sDecode)

End Sub

'******************************************************************************
' Base64デコード
'------------------------------------------------------------------------------
'  第1引数:Base64の文字列
'******************************************************************************
Function decodeToBase64(getBinary As String) As String

    'DOMDocumentオブジェクトの作成
    Dim oXmlNode  As Object
    Set oXmlNode = CreateObject("Msxml2.DOMDocument.3.0").createElement("base64")
    
    'データタイプにbin.base64を設定
    oXmlNode.dataType = "bin.base64"
    
    '引数のBASE64を文字列に変換
    oXmlNode.text = getBinary
    decodeToBase64 = convBinaryToString(oXmlNode.nodeTypedValue)
    
    Set oXmlNode = Nothing
  
End Function

'******************************************************************************
' バイナリを文字列に変換
'------------------------------------------------------------------------------
'  第1引数:バイナリ(Base64)
'******************************************************************************
Function convBinaryToString(getBinary As Variant) As Variant
    
    'ADODB.Streamオブジェクトの作成
    Dim oAdoStm As Object
    Set oAdoStm = CreateObject("ADODB.Stream")
  
    'データ型を設定(1:バイナリデータ)
    oAdoStm.Type = 1
    
    'オブジェクトにバイナリデータを書き込む。
    oAdoStm.Open
    oAdoStm.Write getBinary
    
    'オブジェクト内の位置を設定
    oAdoStm.Position = 0

    'データ型を設定(2:テキストデータ)
    oAdoStm.Type = 2
    
    '文字コードを指定
    oAdoStm.Charset = "utf-8" 'or "shift_jis"

    'データを戻り値として返す
    convBinaryToString = oAdoStm.ReadText
    
    Set oAdoStm = Nothing

End Function

 ■実行結果