Excel VBA - 运行时自动化错误

Excel VBA - 运行时自动化错误

我有下面的 VBA 代码(在这个网站本身以及网络上其他地方很常见,可以在 VBA 中查找 SHA 哈希),它在 Windows 7 32 上的 Excel 2013 中运行良好。

但是,在 Windows 7 32 上的 Excel 2003 SP3 中运行相同的代码时,在 Set asc1 = CreateObject("System.Text.UTF8Encoding") 行处出现运行时自动化错误

我不清楚为什么它在 Excel 2003 中失败。假设它与 dot net framework 有关,我重新安装了 dot Net 4.0,而 3.5.1 已经是 Windows 7 的一部分并且功能已启用。

这个网站上有非常相似的帖子这里但即使它没有解决方案。我还应该进一步排除哪些故障?这可能与缺少或损坏的组件/库或任何相关因素有关吗?

谢谢。

 Public Function SHA1(str1)
    Dim asc1 As Object
    Dim enc1 As Object
    Dim bytes, outstr, pos
    Set asc1 = CreateObject("System.Text.UTF8Encoding")
    Set enc1 = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
    bytes = asc1.GetBytes_4(str1)
    bytes = enc1.ComputeHash_2((bytes))
    outstr = ""

    For pos = 1 To LenB(bytes)
        outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
    Next
    SHA1 = outstr

    Set asc1 = Nothing
    Set enc1 = Nothing


End Function


Private Sub CommandButton1_Click()
Dim txt
txt = TextBox1.Text
TextBox2.Text = SHA1(txt)
End Sub

在此处输入图片描述

答案1

类似的问题在https://stackoverflow.com/questions/375457/cant-instantiate-a-com-object-written-in-c-sharp-from-vba-vb6-ok 在 Stackoverflow.com 上。用户在 Excel VBA 中遇到了相同的“自动化错误”0x80131700。

根据其中发布的答案,我安装了 Office 2003 更新 KB907417http://www.microsoft.com/en-us/download/details.aspx?id=10624 问题已解决。

答案2

可以通过安装.NET Framework 3.5来解决该问题:https://www.microsoft.com/en-ca/download/details.aspx?id=21

相关内容