Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA to call SAP transaction

Tags:

excel

vba

sap-gui

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
like image 626
alam Avatar asked Nov 10 '22 03:11

alam


1 Answers

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!

like image 177
Ted Avatar answered Dec 03 '22 04:12

Ted