vba x64使用ingActiveX报错,'-2147221164 (80040154)'

You can create ActiveX objects like ScriptControl, which available on 32-bit Office versions via mshta x86 host on 64-bit VBA version, here is the example (put the code at the standard VBA project module):

Sub Test()    Dim oSC As Object    Set oSC = CreateObjectx86("MSScriptControl.ScriptControl") ' create ActiveX via x86 mshta host    Debug.Print TypeName(oSC) ' ScriptControl    ' do some stuff    CreateObjectx86 , True ' close mshta host window at the endEnd SubFunction CreateObjectx86(Optional sProgID, Optional bClose = False)    Static oWnd As Object    Dim bRunning As Boolean    #If Win64 Then        bRunning = InStr(TypeName(oWnd), "HTMLWindow") > 0        If bClose Then            If bRunning Then oWnd.Close            Exit Function        End If        If Not bRunning Then            Set oWnd = CreateWindow()            oWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"        End If        Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID)    #Else        Set CreateObjectx86 = CreateObject(sProgID)    #End IfEnd FunctionFunction CreateWindow()    ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356    Dim sSignature, oShellWnd, oProc    On Error Resume Next    sSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38)    CreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""about:<head><script>moveTo(-32000,-32000);document.title='x86Host'</script><hta:application showintaskbar=no /><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & sSignature & "',document.parentWindow);</script></head>""", 0, False    Do        For Each oShellWnd In CreateObject("Shell.Application").Windows            Set CreateWindow = oShellWnd.GetProperty(sSignature)            If Err.Number = 0 Then Exit Function            Err.Clear        Next    LoopEnd Function

It has few shortcomings: the separate mshta.exe process running is necessary, which is listed in task manager, and pressing Alt+Tab hidden HTA window is shown. Also you have to close that HTA window at the end of your code.

(0)

相关推荐