操做思路:
1.使用 ADODB.stream 把字符串轉換爲UFT-8的編碼集存儲到文件 AA
2.使用調用windows命令行CertUtil -hashfile c:\11\AA md5 對文件計算MD5值
3.回顯結果直接輸出到EXCEL
4.字符串截取第二行,去空格,輸出MD5值windows
VBA代碼:ide
strBody = "VBA變量字符串轉換爲UFT-8後MD5加密"編碼
'MD5計算
Dim stream As New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.Charset = "utf-8"
stream.WriteText strBody
' Try to Remove UTF-8 BOM
stream.Position = 3 'skip BOM
Dim BinaryStream As New ADODB.stream
BinaryStream.Type = adTypeBinary
BinaryStream.Mode = adModeReadWrite
BinaryStream.Open
' Strips BOM (first 3 bytes)
stream.CopyTo BinaryStream
stream.Flush
stream.Close
str8 = BinaryStream.Read
BinaryStream.SaveToFile "C:\11\AA", adSaveCreateOverWrite
BinaryStream.Flush
BinaryStream.Close
Dim WSH, wExec, sCmd As String
Set WSH = CreateObject("WScript.Shell")
Set wExec = WSH.Exec("CertUtil -hashfile c:\11\AA md5")
Ar = Split(wExec.StdOut.ReadAll, vbCrLf)
N = UBound(Ar)
Sign = Replace(Ar(1), Space(1), "")
Set wExec = Nothing
Set WSH = Nothing
Range("A12") = strBody加密