Error in Hash Implementation in VBA - Runtime Error -2146232576 (80131700)

二次信任 提交于 2021-02-04 19:27:26

问题


I have implemented the hash method as suggested on the post:

Does VBA has a Hash_HMAC

This is my implementation:

Public Function BASE64SHA1(ByVal sTextToHash As String)
    Dim asc As Object
    Dim enc As Object
    Dim TextToHash() As Byte
    Dim SharedSecretKey() As Byte
    Dim bytes() As Byte

    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")

    TextToHash = asc.GetBytes_4(sTextToHash)
    SharedSecretKey = asc.GetBytes_4(sTextToHash)
    enc.Key = SharedSecretKey

    bytes = enc.ComputeHash_2((TextToHash))
    BASE64SHA1 = EncodeBase64(bytes)

    Set asc = Nothing
    Set enc = Nothing

End Function

Private Function EncodeBase64(ByRef arrData() As Byte) As String

    Dim objXML As Object
    Dim objNode As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
    Set objNode = objXML.createElement("b64")

    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.TEXT

    Set objNode = Nothing
    Set objXML = Nothing

End Function

Everything was working great, running under excel 2013 (Portuguese), windows 8.1 (Portuguese) and windows 7.

Although, when I start using other computer which uses the same Excel 2013 (Portuguese) but the windows 8.1 (English Version), not sure why and if this is the reason, but it came up with the error and the debugger highlight at the first line in BASE64SHA1 function, after variables declaration:

Set asc = CreateObject("System.Text.UTF8Encoding")

Error:

Runtime Error -2146232576 (80131700)

I checked the error messages and came with the details below:

err.Source - VBAProject
err.HelpContext - 1000440
err.HelpFile - C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA7.1\1046\VbLR6.chm
err.LastDllError - 0

Did anyone get the same issue? or could help on that? Looks like I am missing a Reference or something... but I declared as Object and it worked fine in other PC´s... help! =(

Thanks very much!


回答1:


There is an identical error number discussed and the problem solved on a post in MSDN (Excel 2010 VB Run-time Error '-2146232576 (80131700)' Automation Error on CreateObject("System.Collections.ArrayList") ???).

The problem there was solved by installing Microsoft .NET Framework 2.0 SP2 (which automatically also installed 3.5).




回答2:


The error

Runtime Error -2146232576 (80131700)

on CreateObject in a VBA macro is probably due to the lack of a .NET2 framework, as suggested by ZygD. I am answering to introduce a way to check if this is the problem.

You should follow thhese steps:

  1. Open Windows => Run (or hit Windows + R)
  2. Type "CMD"
  3. Push Run
  4. Paste the following register query to check what are the versions of the Framework .NET installed on your PC: reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP"

You will get something like

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\CDF HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0

The istruction CreateObject require a .NET2 version wich means that even if you have the v4.0, you will need NET Framework v3.5 in addition. You can follow this tutorial to install it.

Now, if you follow the same steps listed above you should get

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\CDF HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0

At this point, if you open Microsoft Excel and you hit Alt + F11 and run the macro this should work.



来源:https://stackoverflow.com/questions/28988123/error-in-hash-implementation-in-vba-runtime-error-2146232576-80131700

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!