5

I am trying to call a SAP transaction from Excel, I use many codes from the internet but still a failure. Doesn't matter what code I use, I always get same error message as

A script is opening a connection to system SYSTEM

than error saying:

Runtime error 1000 Error description not avialable"

Than after debug it highlights below in yellow

Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)

its been doing this with all code I tried to pull this.

Code

Dim sap As Object 
Dim conn As Object
 
Sub T_login()
 
    Set sap = CreateObject("SAP.Functions")
    Set conn = sap.Connection
    conn.System = "SYSTEM"
    conn.client = "603"
    conn.user = "LANAX001"
    conn.Password = "alamzeb4"
    conn.Language = "EN"
 
If conn.logon(0, False) Then
    
    MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"
 
Else
 
End If
 
If Not IsObject(SapGuiApp) Then
            
    Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
 
End If
 
If Not IsObject(Connection) Then
 
    Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)
 
End If
 
If Not IsObject(session) Then
 
    Set session = Connection.Children(0)     
    'launch a transaction
    session.findById("wnd[0]").Maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
    session.findById("wnd[0]").sendVKey 0

End Sub
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
alam
  • 51
  • 1
  • 1
  • 2
  • Hello, please check `conn.System = "SYSTEM"` value: as far as I know, it should be 3-symbol identifier like `NSP` or `A4H` -- see connection properties in SAP Logon. – Nikolai Kim Aug 05 '14 at 06:14
  • If You want to connect to SAP via Excel VBA check this post https://stackoverflow.com/a/58348742/11636588 or this article https://simpleexcelvba.com/connect-to-sap-via-excel-vba/ it may help You – Teamothy Dec 03 '19 at 14:42

1 Answers1

1

This is what I use to load data to SAP - verified to work. HTH.

If Not IsObject(App) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject App, "on"
End If

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX" 'XXX = your t-code
... ...

Afterwards, it depends on what you do in the t-code.

Good luck!

Ted
  • 33
  • 6