Excel VBA SHA1函数

我运行这个模块,但它不断给我一个错误,debugging把我发送到这一行:

Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") 

运行时错误'-2146233079(80131509':自动化错误)

我添加了对mscorlibtypes库的引用,但仍然不起作用。

 Public Function SHA1(sIn As String, Optional bB64 As Boolean = 0) As String 'Set a reference to mscorlib 4.0 64-bit 'Test with empty string input: '40 Hex: da39a3ee5e6...etc '28 Base-64: 2jmj7l5rSw0yVb...etc Dim oT As Object, oSHA1 As Object Dim TextToHash() As Byte Dim bytes() As Byte Set oT = CreateObject("System.Text.UTF8Encoding") Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") TextToHash = oT.GetBytes_4(sIn) bytes = oSHA1.ComputeHash_2((TextToHash)) If bB64 = True Then SHA1 = ConvToBase64String(bytes) Else SHA1 = ConvToHexString(bytes) End If Set oT = Nothing Set oSHA1 = Nothing End Function 
 Private Function ConvToBase64String(vIn As Variant) As Variant Dim oD As Object Set oD = CreateObject("MSXML2.DOMDocument") With oD .LoadXML "<root />" .DocumentElement.DataType = "bin.base64" .DocumentElement.nodeTypedValue = vIn End With ConvToBase64String = Replace(oD.DocumentElement.Text, vbLf, "") Set oD = Nothing End Function Private Function ConvToHexString(vIn As Variant) As Variant Dim oD As Object Set oD = CreateObject("MSXML2.DOMDocument") With oD .LoadXML "<root />" .DocumentElement.DataType = "bin.Hex" .DocumentElement.nodeTypedValue = vIn End With ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "") Set oD = Nothing End Function 

我在代码中改变了这一点

 'Set oT = CreateObject("System.Text.UTF8Encoding") 'Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") With New UTF8Encoding TextToHash = .GetBytes_4(sIn) End With With New SHA1Managed bytes = .ComputeHash_2((TextToHash)) End With 

现在我得到了同样的错误,但在新的SHA1Managed

这是我现在的代码,但仍然在与新的SHA1Managed错误

公共函数SHA1(sIn作为string,可选bB64作为布尔= 0)作为string'设置refe

 'Test with empty string input: '40 Hex: da39a3ee5e6...etc '28 Base-64: 2jmj7l5rSw0yVb...etc Dim oT As Object, oSHA1 As Object 'Dim TextToHash() As Byte 'Dim bytes() As Byte 'Set oT = CreateObject("System.Text.UTF8Encoding") 'Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") With New UTF8Encoding TextToHash = .GetBytes_4(sIn) End With With New SHA1Managed bytes = .ComputeHash_2((TextToHash)) End With TextToHash = oT.GetBytes_4(sIn) bytes = oSHA1.ComputeHash_2((TextToHash)) If bB64 = True Then SHA1 = ConvToBase64String(bytes) Else SHA1 = ConvToHexString(bytes) End If Set oT = Nothing Set oSHA1 = Nothing 

结束function

项目引用对话框

我改变了参考v2.05 mscorlib.tlb,但我仍然得到相同的错误

在这里输入图像说明

在这里输入图像说明

如果引用types库,则不需要使用CreateObject在该库中创build任何实例。

我不是真正了解后期约束 – 亚历克斯·米哈伊洛夫7分钟前

这是发生的地方:

 Dim oT As Object, oSHA1 As Object 

通过使用Object接口,VBA没有对象成员的编译时知识; 反对它的请求将迟到 ,即在运行时解决。 你不会得到智能感知 ,而且VBA会编译你在调用成员时所犯的任何错误或错误:你只会在运行时知道有什么地方是错误的。

早期绑定意味着你正在使用的typesVBA在编译时知道:你得到IntelliSense的一切,VBA将拒绝编译错字或成员调用,比如缺less非可选参数。

那么这样的事情:

 Sub test() Dim inputBytes() As Byte With New UTF8Encoding inputBytes = .GetBytes_4("TEST") End With Dim outputBytes() As Byte With New SHA1Managed outputBytes = .ComputeHash_2((inputBytes)) End With Debug.Print ConvToHexString(outputBytes) End Sub 

注意With New块负责创build和销毁对象引用,所以你甚至不需要局部variables。


.NET 2.0框架中引用mscorlib.tlbtesting(显然使用32位或64位版本) – 即时窗格(Ctrl + G)输出:

 Sheet1.Test 984816fd329622876e14907634264e6f332e9fb3 

尝试删除引用,好的对话框,然后再次启动并浏览C:\Windows\Microsoft.NET\Framework\v2.0.50727 ,并selectmscorlib.tlb (而不是.dll )。