3

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 another computer which uses the same Excel 2013 (Portuguese) but the Windows 8.1 (English), not sure why and if this is the reason, but it came up with the error and the debugger highlighted 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

Can anyone help? Looks like I am missing a Reference or something... but I have declared as Object, and it worked fine on other PCs...

ZygD
  • 22,092
  • 39
  • 79
  • 102
Evis
  • 571
  • 8
  • 22
  • @JohnGreen Sounds like that user has a missing/corrupted dll. According to MSDN, that class should be part of mscorlib.dll – Degustaf Mar 12 '15 at 20:20

2 Answers2

3

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.

Nicolaesse
  • 2,554
  • 12
  • 46
  • 71
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).

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • But is this the only possible solution? Could the author have used any different object instead? What could be done to avoid installing additional packages? To "install additional packages" is really frustrating for the end users. – 71GA Aug 01 '22 at 13:54