4

This is more of general question, I suppose, as-well-as help with a specific line of code.

I have an Excel file that I was working on just a few days ago that was working fine, however now whenever I try to run the macro in the workbook to pull data from a website, I receive the error "Run-time error '380': A script engine for the specified language can not be created."

Here is the code block where I am running into the issue. I have starred the specific section where the error is thrown.

Dim H As Object, S As Object, jParse As Object, X64 As Object, i&

Set H = CreateObject("WinHTTP.WinHTTPRequest.5.1")
    H.SetAutoLogonPolicy 0

#If Win64 Then
    Set X64 = x64Solution()
    X64.execScript "Function CreateObjectx86(sProgID) Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"
    Set S = X64.CreateObjectx86("MSScriptControl.ScriptControl")
#Else
    Set S = CreateObject("ScriptControl")
#End If
    ***S.Language = "JScript"***
    S.AddCode "function keys(O) { var k = new Array(); for (var x in O) { k.push(x); } return k; } "

I have never seen this error before and I am not sure how to fix this issue. I have looked online and have thus far been unsuccessful in figuring out the problem. I have also tried downloading and installing the zip file from Microsoft in this link: https://gallery.technet.microsoft.com/scriptcenter/Registry-key-to-re-enable-835fba77 with no success.

Any help would be appreciated, because I really don't know what to do here.

Also if Stack Overflow is not really the place for this kind of question, any help in directing me somewhere that would be better suited for this kind of problem would be appreciated.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Dev-MDW2
  • 113
  • 9

1 Answers1

2

I just had a similar encounter attempting to use JScript to parse some JSON from the SO API using a x64 machine.

Disclaimer: I did not author the following procedures, but unfortunately I do not have the source of where I obtained them either.

As you've probably already figured out, MSScriptControl.ScriptControl doesn't like the x64 architecture very well. Here are a couple of functions that will allow you to do what you need.

I placed these in a separate module:

Public Function 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 If

End Function

Private Function CreateWindow()

    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
    Loop

End Function

Then you can go back to your S object and set it this way:

Dim S As Object
Set S = CreateObjectx86("MSScriptControl.ScriptControl")

S.Language = "JScript"
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • Thanks for the code and taking the time to help. Where should I be injecting this into my code? So far I am getting an automation error when running the script. – Dev-MDW2 Dec 26 '18 at 14:30
  • Check updated [code here](https://stackoverflow.com/a/38134477/2165759) in case permission issues with `Scriptlet.TypeLib` – omegastripes Sep 09 '20 at 19:28