OWnd.execScript "setInterval('Check() ', 500) " OWnd.execScript "Sub Check(): On Error Resume Next: Lost = True: App.Run(""CreateObjectx86""): If Lost And (Err.Number = 1004 Or Err.Number = 0) Then close: End If End Sub", "VBScript" OWnd.execScript "var Lost, App ": Set oWnd.App = Application ' if necessary you can manually close mshta host window by CreateObjectx86 Emptyįunction CreateObjectx86(Optional sProgID) ' mshta window is running until Static oWnd reference to window lost
XML TOOLS ADD IN EXCEL 2010 64 BIT CODE
Such actions exceptions are handled, and after completion the code will continue to work, no crashes.
XML TOOLS ADD IN EXCEL 2010 64 BIT WINDOWS
Note: VBA breakpoints (error 57097), worksheet cells edited by user, opened dialog modal windows like Open / Save / Options (error -2147418111) will suspend the tracing since they makes application unresponsive for external calls from mshta. The point is that mshta window checks the state of VBA's Static oWnd variable calling CreateObjectx86 without argument via internal setInterval() function each 500 msec, and quits if the reference lost (either user have pressed Reset in VBA Project window, or the workbook has been closed (error 1004)). Second method for those who don't want to use classes for some reason. ' if necessary you can manually close mshta host window by oHost.Quit ' mshta window is running until oHost instance exists Set oSC = oHost.CreateObjectx86("ScriptControl") ' create ActiveX via x86 mshta host Put the below code in a standard module: Option Explicit If InStr(TypeName(oWnd), "HTMLWindow") > 0 Then oWnd.Close Set CreateObjectx86 = CreateObject(sProgID) If InStr(TypeName(oWnd), "HTMLWindow") = 0 Then Class_Initialize OWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID) End Function", "VBScript" Put the below code in a class module named cMSHTAx86Host: Option Explicit Note: if Excel crashes while code execution then there is no class termination, so the window will stay in background. You can make the host window to be closed automatically: by creating class instance or mshta active tracing.įirst method assumes you create a class instance as a wrapper, which uses Private Sub Class_Terminate() to close the window.
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:Īlso you have to close that HTA window at the end of your code by CreateObjectx86 Empty. Set CreateWindow = oShellWnd.GetProperty(sSignature) SSignature = sSignature & Hex(Int(Rnd * 16))ĬreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""moveTo(-32000,-32000) document.title='x86Host'shell.putproperty('" & sSignature & "',document.parentWindow) """, 0, Falseįor Each oShellWnd In CreateObject("Shell.Application").Windows If Not IsEmpty(sProgID) Then Set CreateObjectx86 = CreateObject(sProgID) Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID) OWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"
Set oSC = CreateObjectx86("ScriptControl") ' create ActiveX via x86 mshta hostĭebug.Print TypeName(oSC) ' ScriptControlĬreateObjectx86 Empty ' close mshta host window at the endīRunning = InStr(TypeName(oWnd), "HTMLWindow") > 0 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 in a standard VBA project module): Option Explicit