Skip to Content
-1
May 29, 2020 at 09:19 PM

OPEN SAP WITH EXCEL

772 Views Last edit May 29, 2020 at 06:35 PM 2 rev

Hi!

I'm new at creating scripts on SAP. I've been trying to record a script in vba Excel but I keep getting de error 424 "object required" in the line where Excel is supossed to open the SAP System (P08 One SAP)

Can someone please help me?

This is the script that I have so far:

Sub aa()
Dim A As String
Dim D As Date
Dim B As String
Dim Y As Date

Application.DisplayAlerts = False

Shell "C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe", vbNormalFocus

Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon")
Application.Wait Now + TimeValue("0:00:01")
Loop

If Not IsObject(SAPguiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SapGuiAuto = SapGuiAuto.GetScriptingEngine
End If

Here is where i get the error 424:

If Not IsObject(Connection) Then
Set Connection = SAPguiApp.OpenConnection("P08 One SAP", True)
End If

If Not IsObject(Session) Then
Set Session = Connection.Children(0)
End If

If IsObject(WScript) Then
WScript.ConnectObject Session, "on"
WScript.ConnectObject SAPguiApp, "on"
End If

On Error Resume Next
If Session.findById("wnd[1]").Text = "License Information for Multiple Logon" Then
Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").SetFocus
Session.findById("wnd[1]").sendVKey 0
End If

Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nlt22"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/radT3_QUITA").Select
Session.findById("wnd[0]/usr/chkT3_SEVON").Selected = True
Session.findById("wnd[0]/usr/ctxtT3_LGNUM").Text = "mx1"
Session.findById("wnd[0]/usr/ctxtT3_LGTYP-LOW").Text = "001"
Session.findById("wnd[0]/usr/ctxtBDATU-LOW").SetFocus
Session.findById("wnd[0]/usr/ctxtBDATU-LOW").caretPosition = 0
Session.findById("wnd[0]").sendVKey 4

Y = Now() - 1
D = Now()
A = Year(Y) & Format(Month(Y), "00") & Format(Day(Y), "00")
B = Year(D) & Format(Month(D), "00") & Format(Day(D), "00")

Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").Text = A
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "A,A"
Session.findById("wnd[0]/usr/ctxtBDATU-HIGH").SetFocus
Session.findById("wnd[0]/usr/ctxtBDATU-HIGH").caretPosition = 0
Session.findById("wnd[0]").sendVKey 4
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").Text = B
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "B,B"
Session.findById("wnd[0]/usr/ctxtLISTV").Text = "/AUXIA"
Session.findById("wnd[0]/usr/ctxtLISTV").SetFocus
Session.findById("wnd[0]/usr/ctxtLISTV").caretPosition = 6
Session.findById("wnd[0]").sendVKey 8
Session.findById("wnd[0]/usr/lbl[23,5]").SetFocus
Session.findById("wnd[0]/usr/lbl[23,5]").caretPosition = 0
Session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[1]").Select
Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "G:\Common\DPP\@Site Logistics\Almacen\MACRO VENTANILLA\"
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "closed TOs.XLSX"
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 15
Session.findById("wnd[1]/tbar[0]/btn[11]").press
Session.findById("wnd[0]/tbar[0]/btn[15]").press
Session.findById("wnd[0]/tbar[0]/btn[15]").press

End Sub