VBA Run-time error '380': A script engine for the specified language can not be created - excel

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.

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"

Related

why I am getting an error on internet explore element?

Why am I getting an error?
Object doesn't support this property or method
getElementsByClassName is only defined on document, not HTMLElement. Instead use querySelectorAll.
You should also use intermediate variables so you can inspect your program when debugging without having multiple calls on the same line and then not-knowing which specific call failed.
Like so:
Dim contentElement
Set contentElement = IE.GetElementById( "content" )
If contentElement = Nothing Then
MsgBox( "<div#content> not found" )
End If
Dim postTitles
Set postTitles = contentElement.QuerySelectorAll( ".post-title" )
Dim postTitle
For Each postTitle in postTitles
' do stuff
Next

Is there a resolution in VBA for the error Run-time error '429' when trying to connect to a website?

I am trying to create a VBA to download a google sheet into excel so I can compile stock market data daily. I would simply use power query for this but I am doing this on my personal laptop which is a mac and does not support power query. I am relatively new to coding so have been leaning on following online instructions. The instruction includes this:
Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")
This line when ran creates an error message saying:
"
Run-time error '429':
ActiveX component can't create object
"
I think the issue lies within the fact that the instruction is based on a windows operating system. Any solution I've searched for is specific to windows operating systems.
Does anybody here know if I can change the "MSXML2.XMLHTTP.3.0" part of my code to fit it better to mac? Not sure if this is what needs to be done but any guidance would be super appreciated.
I attached my full code below but feel free to ignore it if not relavent. Thank you!!
Sub DownloadGoogleSheets()
Dim ShtUrl As String, Location As String, FileName As String
Dim objWebCon, objWrit As Object
'Sheet Url
ShtUrl = "https://docs.google.com/spreadsheets/d/1wpA_epxtlz96sxETqKttJwsy9Aubb15H8xslcSQ20T0/export?format=csv&id=1wpA_epxtlz96sxETqKttJwsy9Aubb15H8xslcSQ20T0" & gid = 1319327791
'Location
Location = ThisWorkbook.Path & "\" '/Users/[myName]/Desktop/Stock Analysis/n"
'FileName
FileName = "GoogleSheet.csv"
'Connection to Website
Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")
'Writer
Set objWrit = CreateObject("ADODB.Stream")
'Connecting to the Website
objWebCon.Open "Get", ShtUrl, False
objWebCon.Send (ShtUrl)
'Once page is fully loaded
If objWebCon.Status = 200 Then
'Write the text of the sheet
objWrit.Open
objWrit.Type = 1
objWrit.Write objWebCon.ResponseBody
objWrit.Position = 0
objWrit.SaveToFile Location & FileName
objWrit.Close
End If
Set objWebCon = Nothing
Set objWrit = Nothing
End Sub

VBA program executing SAP RFCs works in Excel 2016 on-prem, but not in Office 365 (Run-time error 20080008...Bad Variant Type)

Let me first say that I saw the other two threads that mentioned this issue here and here, but they didn't help me solve my problem.
I've been testing a program for several weeks in the on-prem Excel 2016 environment (32-bit) with no problems. My company is making the move to Office 365 soon, so I decided to test it over there as well. On that system, I'm getting a run-time error on the line Functions.Connection = objConnection
Option Explicit
Public Functions As SAPFunctionsOCX.SAPFunctions
Private LogonControl As SAPLogonCtrl.SAPLogonControl
Private objConnection As SAPLogonCtrl.Connection
Public Func As SAPFunctionsOCX.Function
Public Commit As SAPFunctionsOCX.Function
Public TableFactory As SAPTableFactory
Public silentLogon As Boolean
Public tblReadTableOptions, tblReadTableFields, tblReadTableData As SAPTableFactoryCtrl.Table
Sub ExtractProjectData()
If objConnection Is Nothing Then LogonToSAP
InitiateSAPVariables
Set Func = Functions.Add("BBP_RFC_READ_TABLE")
Set tblReadTableOptions = Func.Tables("OPTIONS")
Set tblReadTableFields = Func.Tables("FIELDS")
Set tblReadTableData = Func.Tables("DATA")
'extract/transform data from SAP tables
End Sub
Function InitiateSAPVariables()
Set Functions = Nothing
Set TableFactory = Nothing
Set Func = Nothing
Set Functions = CreateObject("SAP.Functions")
Set TableFactory = CreateObject("SAP.TableFactory.1")
Functions.Connection = objConnection 'run-time error here in Office 365 but not in on-prem
End Function
Function LogonToSAP()
Dim establishConnection As Boolean
silentLogon = false
Set LogonControl = CreateObject("SAP.LogonControl.1")
Set objConnection = LogonControl.NewConnection
objConnection.Client = "###"
objConnection.Language = "EN"
objConnection.SystemNumber = "##"
objConnection.User = ""
objConnection.Password = ""
objConnection.HostName = "###############"
objConnection.System = "###"
objConnection.ApplicationServer = "###.###.#.##"
establishConnection = objConnection.Logon(0, silentLogon)
End Function
A quick check of objConnection tells me that logon was successful...so I know that part is working on 365. For some reason though, it doesn't like assigning the Connection property of the Functions SAPFunctionsOCX.SAPFunctions object in the 365 environment (please feel free to correct my verbiage on that...I know it's not quite right).
Note that I'm not seeing any reference issues nor am I getting any compile errors in either environment. The first sign of trouble is on execution of Functions.Connection = objConnection
There's one more twist here and that is that I have another older VBA program that logs into SAP and runs remote function calls that doesn't use SAPFunctionsOCX.SAPFunctions, but rather declares variable R3 as Public R3 As Object and then sets R3 later in the logon code as Set R3 = CreateObject("SAP.Functions")...it does not use OCX. In other words, the old routine uses late binding. When the Functions object (R3 in this case) is set this way, I am able to run RFCs in both on prem and Office 365 environments.
Function LogonProdSAP(Optional SuppressLoginScreen As Boolean)
Application.ScreenUpdating = False
'**********************************************
'Create Server object and Setup the connection for DEV
'**********************************************
Set R3 = CreateObject("SAP.Functions")
If SuppressLoginScreen Then
R3.Connection.System = "###"
R3.Connection.HostName = "###################"
R3.Connection.SystemNumber = "##"
R3.Connection.Client = "###"
R3.Connection.User = "##########"
R3.Connection.Password = "#########"
R3.Connection.Language = "EN"
' Call Logger("LogonProdSAP> " & GetUserName)
End If
LogonProdSAP = R3.Connection.logon(0, SuppressLoginScreen)
If LogonProdSAP <> True Then MsgBox ("Logon error"): Exit Function
End Function
I could just go back to doing it this way, but I'd rather not have to reconfigure all of the code I just set up. In addition, I prefer binding early so Intellitype works to show all properties/methods available to that object. I'm sure there are other benefits as well.
What do I have to do to get the early-binding technique to work on Office 365?
It's due to the fact that your Office is in 64 bits version, and SAP GUI for Windows up to version 7.60 is in 32 bits (next SAP GUI version 7.70 should be in 64 bits, so it should work again).
You have a workaround to make VBA work with SAP GUI 32-bits DLL, by using DLL Surrogate, i.e. by editing the Windows Registry of all incompatible SAP GUI DLL. The original solution was proposed here at SAP Community.
To simplify the task, you may create my .REG file, execute it to update automatically the Windows Registry, and your VBA macro should then work.
I duplicate here my .REG file:
; ====================================================================================
; SAP Logon Unicode Control %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtlogU.ocx {0AAF5A11-8C04-4385-A925-0B62F6632BEC}
; ====================================================================================
[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]
"AppID"="{0AAF5A11-8C04-4385-A925-0B62F6632BEC}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]
; ====================================================================================
; SAP Remote Function Call Unicode Control %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtfuncu.ocx {0AF427E7-03B9-4673-8F21-F33A683BCE28}
; ====================================================================================
[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]
"AppID"="{0AF427E7-03B9-4673-8F21-F33A683BCE28}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]
; ====================================================================================
; SAP Logon Control (not Unicode) %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtlog.ocx {B24944D6-1501-11CF-8981-0000E8A49FA0}
; ====================================================================================
[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]
"AppID"="{B24944D6-1501-11CF-8981-0000E8A49FA0}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]
; ====================================================================================
; SAP Remote Function Call Control (not Unicode) %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtfuncs.ocx {5B076C03-2F26-11CF-9AE5-0800096E19F4}
; ====================================================================================
[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]
"AppID"="{5B076C03-2F26-11CF-9AE5-0800096E19F4}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]

excel VBA web service give error METHOD OR DATAOBJECT NOT FOUND

I have a weird problem with an excel VBA application.
It uses a web service to connect to a mysql database.
it all runs well in most computers....however in some computers it throws an error:
ERROR WHEN COMPILING "METHOD OR DATAOBJECT NOT FOUND"
All computers are running Windows 7 and Office 2007.
Here is an example of the web service code (where the error happens)
Public Function wsm_get_portscountries(ByVal str_SQLQuery As String) As MSXML2.IXMLDOMNodeList
On Error GoTo wsm_get_portscountriesTrap
Set wsm_get_portscountries = sc_WebService1.wsm_get_portscountries(str_SQLQuery)
Exit Function
wsm_get_portscountriesTrap:
WebService1ErrorHandler "wsm_get_portscountries"
End Function
The error highlights the bolded part here:
sc_WebService1.wsm_get_portscountries(str_SQLQuery)
Has anyone encountered the same problem ?? Any ideas ??
As I said, on most computers it runs well, no problems, it only happens with some.
Thanks in advance.
additional information:
class module
Private Sub Class_Initialize()
On Error GoTo Class_Initialize_error
Dim str_WSML As String
str_WSML = ""
Set sc_WebService1 = New SoapClient30
sc_WebService1.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE
sc_WebService1.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
sc_WebService1.ConnectorProperty("EnableAutoProxy") = True
Exit Sub
Class_Initialize_error:
End Sub

SAP RFC call returns "Error 0" in RETURN parameter from vb

Hi everybody and thanks in advance.
I'm trying to call a SAP BAPI using RFC from vb but I'm having some problem to get the result of the call.
The BAPI "BAPI_GL_ACC_EXISTENCECHECK" (from General Ledger Account module) has two parameters,
COMPANYCODE and GLACCT, and a RETURN parameter.
I wrote this piece of code to make the call and I had no problem to establish the SAP Connection (I use the SAP Logon Control OLE/COM object to do the job), and I tried to make the RFC call.
Also in this case I make the call without problems (it seems, not sure about it ...), because the RFC call returns true and no exception.
However, looking through the objReturn object/parameter, it has a value "Error 0" in it.
I was expecting a complex structure like the BAPIRETURN object in SAP or something similar if the account doesn't exist.
Tried to search with Google and SAP forums but I haven't found a real solution to my problem, so here I am to ask you all if you have some idea to solve this problem (maybe I'm only making a wrong call!!! I'm quite a newbie on SAP integration ...).
BTW, Final Notes: after a lot of RFC_NO_AUTHORIZATION on the SAP side, they gave me a SAP_ALL / S_RFC authorization (sort of, not a SAP expert) and the error RFC_NO_AUTHORIZATION disappeared, but not the Error 0 return
Dim sapConn As Object
Dim objRfcFunc As Object
Dim SAPMandante As String
Dim SAPUtente As String
Dim SAPPassword As String
Dim SAPLingua As String
Dim SAPApplicationServer As String
Dim SAPNumeroSistema As Variant
Dim SAPIDSistema As String
Dim SAPRouter As String
Dim FlagInsertLogin As Integer
Dim FlagLogin As Variant
On Error GoTo ErrorHandler
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object
'Silent Logon
SAPMandante = "xxx"
SAPUtente = "yyyy"
SAPPassword = "zzzzzz"
SAPLingua = "IT"
SAPApplicationServer = "www.xxx.com"
SAPNumeroSistema = x
SAPIDSistema = "zzz"
SAPRouter = ""
FlagLogin = SilentLogin(sapConn, SAPMandante, SAPUtente, SAPPassword, SAPLingua, SAPApplicationServer, SAPNumeroSistema, SAPIDSistema, SAPRouter) 'IT WORKS, NO PROBLEM HERE
If FlagLogin = False Then
'Explicit Logon
If sapConn.Connection.logon(0, False) <> True Then
MsgBox "Cannot Log on to SAP", 16, "Query Interrupted"
sapConn.Connection.logoff
Set sapConn = Nothing
InsertCash = False
Exit Sub
End If
End If
'BAPI RFC Call
Set objRfcFunc = sapConn.Add("BAPI_GL_ACC_EXISTENCECHECK")
objRfcFunc.exports("COMPANYCODE") = "C100"
objRfcFunc.exports("GLACCT") = "0000000001" 'Inexistent
Rem *** BAPI CALL ***
If objRfcFunc.Call = False Then
ErrorMsg = objRfcFunc.Exception 'Message collection
MsgBox ErrorMsg, 16, "Errore"
sapConn.Connection.logoff
Exit Sub
else
Dim objReturn As Object
Set objReturn = objRfcFunc.imports("RETURN")
End If
You need to put
Dim objReturn As Object
Set objReturn = objRfcFunc.imports("RETURN")
BEFORE objRfcFunc.Call
i.e. you must state what you're importing from the function before you call it. I usually put it alongside the .exports() lines.
The problem was solved, please take a look at this thread ...
http://sap.ittoolbox.com/groups/technical-functional/sap-dev/sap-rfc-call-returns-error-in-return-parameter-from-vb-before-the-rfc-call-4894968#M4902486

Resources