2017-09-29 164 views
0

我跑這個模塊,但它不斷給我一個錯誤和調試送我去這條線:Excel的VBA SHA1功能

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

運行時錯誤「-2146233079(80131509」:自動化錯誤)

我添加了對mscorlib類型庫的引用,但它仍然不起作用。

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

這是我怎麼有碼現在,但仍然出現與新的SHA1管理錯誤

公共功能SHA1(SIN作爲字符串,可選bB64由於布爾= 0)作爲字符串 「設置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 

端功能

project references dialog

我改爲V2.05 mscorlib程序參考.TLB,但我仍然得到同樣的錯誤

enter image description here

enter image description here

+0

你得到了什麼確切的錯誤? –

+0

運行時錯誤'-2146233079(80131509':自動化錯誤 –

+1

https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA,這是我發現此代碼的方式 –

回答

2

如果您正在引用類型庫,則不需要使用CreateObject在該庫中創建任何內容的實例。

我真的不理解後期綁定 - 亞歷克斯·米哈伊洛夫7分鐘前

這是它發生的事情:

Dim oT As Object, oSHA1 As Object 

通過對工作VBA對象的成員沒有編譯時知識,針對它的呼叫將是遲到的,即在運行時解決。你不會得到智能感知,VBA會編譯你在調用它的成員時所犯的任何錯字或錯誤:你只會在運行時知道出了什麼問題。

早期綁定意味着你有兩種工作VBA知道在編譯時:你智能感知的一切,和VBA將拒絕編譯一個錯字,或者,說一員通話,缺非可選參數。

事情是這樣的,那麼:

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塊負責創建和銷燬的對象引用的照顧,所以你甚至不需要局部變量他們。


.NET 2.0框架與基準測試以mscorlib.tlb(可與明顯32個或64位版本) - 立即窗格(按Ctrl + G)輸出:

Sheet1.Test 
984816fd329622876e14907634264e6f332e9fb3 

嘗試移除參考,確定對話框,然後再次提起並瀏覽C:\Windows\Microsoft.NET\Framework\v2.0.50727,並選擇mscorlib.tlb(而不是.dll)。

+0

我做了這些修改'Set oT = CreateObject(「System.Text.UTF8Encoding」) 'Set oSHA1 = CreateObject(「System.Security.Cryptography.SHA1Managed」) With New UT F8Encoding TextToHash = .GetBytes_4(SIN) 尾隨着 隨着新SHA1Managed 字節= .ComputeHash_2((TextToHash)) 尾隨着 –

+0

哥們,整點'New'ing備份的對象是,你不需要'CreateObject'它們。對不起,如果我的回答不夠清楚......只需刪除這些'CreateObject'調用,並通過查看mscorlib類型庫的可用內容,在* Object Browser *中驗證您使用的是正確類型( F2)。 –

+0

嘿芒非常感謝所有幫助,我該怎麼做代碼 –