问题
I have been initializing a 2D array using Arr=Array(Array(),Array())
.
But I have found that a Run Time Error 5 - Invalid Procedure Call or Argument would be triggered.
The code have been used for years and the problem arose yesterday so I think it may be the result of some of the updates for MS Office or Windows.
Update: The Error exists for Windows 10, Windows 7 but NOT for Windows Vista.
To reproduce the Error:
Sub foo()
Dim Arr As Variant
Arr = Array(Array(), Array())
End Sub
回答1:
This is due to a bug discovered in the security update that was pushed by Microsoft on 8/13/2019 (https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2019-1182)
Look for the KB number related to your version of Windows and type (32/64 bit) and uninstall the update. This will fix the issue.
Microsoft is looking into fixing the bug in the update.
Update
Microsoft just released a patch: https://support.microsoft.com/en-us/help/4517298/windows-8-1-update-kb4517298
回答2:
There is a workaround for creating empty arrays.
You can use the SafeArrayCreateVector API-Function
Private Declare Function EmptyVariantArr Lib "oleaut32" _
Alias "SafeArrayCreateVector" (Optional ByVal VT As VbVarType = vbVariant, _
Optional ByVal LB& = 0, Optional ByVal cElements& = 0) As Variant()
Dim A()
A() = EmptyVariantArr
来源:https://stackoverflow.com/questions/57504012/run-time-error-5-when-initializing-a-2d-array