默认策略是阻止CLR 4从CLR 2中执行遗留代码:
Set clr = New mscoree.CorRuntimeHost
要启用旧版执行,您可以
excel.exe.config在以下位置的文件夹中创建文件
excel.exe:
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup></configuration>
或者,您可以调用本机函数
CorBindToRuntimeEx而不是
New mscoree.CorRuntimeHost:
Private Declare PtrSafe Function CorBindToRuntimeEx Lib "mscoree" ( _ ByVal pwszVersion As LongPtr, _ ByVal pwszBuildFlavor As LongPtr, _ ByVal startupFlags As Long, _ ByRef rclsid As Long, _ ByRef riid As Long, _ ByRef ppvObject As mscoree.CorRuntimeHost) As LongPrivate Declare PtrSafe Function VariantCopy Lib "oleaut32" (dest, src) As Long''' Creates a .Net object with the CLR 4 without registration. '''Function CreateInstance(assembly As String, typeName As String) As Variant Const CLR$ = "v4.0.30319" Static domain As mscorlib.AppDomain If domain Is Nothing Then Dim host As mscoree.CorRuntimeHost, hr&, T&(0 To 7) T(0) = &HCB2F6723: T(1) = &H11D2AB3A: T(2) = &HC000409C: T(3) = &H3E0AA34F T(4) = &HCB2F6722: T(5) = &H11D2AB3A: T(6) = &HC000409C: T(7) = &H3E0AA34F hr = CorBindToRuntimeEx(StrPtr(CLR), 0, 3, T(0), T(4), host) If hr And -2 Then err.Raise hr host.Start host.GetDefaultDomain domain End If VariantCopy CreateInstance, domain.CreateInstanceFrom(assembly, typeName).UnwrapEnd Function



