In all of my SAP based articles I showed You how to open SAP Logon, choose the environment, go to transaction, do whatever You need inside and then close the environment. In this one I will present to You the last thing, which I never show You before – how to close SAP Logon window.

Let’s just remind ourselfs every SAP connection code initialization, which I probably used in every article code.
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
Do Until WshShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WshShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("Connection/Environment name", True)
Set session = Connection.Children(0)
So starting from the first line, it:
1. Opens SAP Logon window from the given path using Shell function.
2. Creates WScript object to get activated “SAP Logon” in the title bar (doing that in the loop).
3. Gets SAPGUI into object (which is basically SAP Logon window) and connects into given Environment.
In this article I will focus on first 2 points.
Be more specific
In given example I declared & used WScript.Shell object to wait until the SAP application is activated – waits till it is fully loaded. Let’s use it earlier and put the SAP application into variable.
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
Dim SAP As Object
Set SAP = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
Do Until WshShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WshShell = Nothing
So basically using WScript.Shell object I launched the SAP application and set that into SAP object.
You may ask
Why use another object if You still using only Wscript object to wait for SAP?
I’m glad You asked.
It is helpful in the end to close the SAP Logon window after all transactions are done.
Shell "taskkill /pid " & SAP.ProcessID
I just read the ID of the SAP object, created in the beginning of the code and told to close – taskkill – the process. And just like that the SAP Logon window is closed.
Close SAP Logon window using user32 library functions
Another cool method to close SAP Logon is to use functions from system library. In this case it is user32 library.
To make magic happened You need 2 functions:
– FindWindow
– SendMessage
and 1 constant:
– WM_CLOSE
Option Explicit
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Const WM_CLOSE = &H10
Sub findAndCloseSAPWindow()
Dim sapGUIWindow As LongPtr
sapGUIWindow = FindWindow(vbNullString, "SAP Logon 750")
SendMessage sapGUIWindow, WM_CLOSE, 0, 0
End Sub
First of all find the window ID by name using FindWindow function. Then close the window using found window ID sapGUIWindow and constant WM_CLOSE in the SendMessage function.
A little bit longer, but also effective like the first method.
Unfortunately You can use that only if You know the version of SAP – whole title top bar, because You need to pass whole phrase to the function, not part of it.
Bonus
I also found the solution, where there is used SendKeys method. It looks OK, but not really convinced to use that. Sometimes it can ALT+F4 close not the SAP Logon window, but other things.
Just showing that as a fun fact, not really as a solution.
Could you please send me the full code from starting (opening, transaction script, then how to close logon). I’m not able to figure out how to enter this close log on coding. Could you please send one example from opening logon to closing logon.
Thanks in advance!
Below is my code, where i can enter the close SAP logon coding.
Sub SapConn()
Dim Appl As Object
Dim Connection As Object
Dim session As Object
Dim WshShell As Object
Dim SapGui As Object
‘Of course change for your file directory
Shell “C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe”, 4
Set WshShell = CreateObject(“WScript.Shell”)
Do Until WshShell.AppActivate(“SAP Logon “)
Application.Wait Now + TimeValue(“0:00:01”)
Loop
Set WshShell = Nothing
Application.Wait (Now + TimeValue(“00:00:01”))
Set SapGui = GetObject(“SAPGUI”)
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection(“SG00 – P34: SHAPE Production ERP (with SSO)”, True)
Set session = Connection.Children(0)
If session.Children.Count > 1 Then
answer = MsgBox(“You’ve got opened SAP already,” & _
“please leave and try again”, vbOKOnly, “Opened SAP”)
session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).Select
session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).SetFocus
session.findById(“wnd[1]/tbar[0]/btn[0]”).press
End If
Dim LastRow
Dim sht As Worksheet
Dim Ord As Worksheet
Dim IW39 As Worksheet
Dim WBS As Worksheet
Dim List As Worksheet
Folderpath = ActiveWorkbook.ActiveSheet.Range(“B4”).Value
Period = ActiveWorkbook.ActiveSheet.Range(“B3”).Value
Period1 = ActiveWorkbook.ActiveSheet.Range(“C3”).Value
Set sht = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\CC.xlsx”).Worksheets(“Sheet1”)
LastRow = sht.Range(“A2”).CurrentRegion.Rows.Count
sht.Range(“A2:A” & LastRow).Copy
session.findById(“wnd[0]”).maximize
session.findById(“wnd[0]/tbar[0]/okcd”).Text = “/niw39”
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtDATUV”).Text = “”
session.findById(“wnd[0]/usr/ctxtDATUB”).Text = “”
session.findById(“wnd[0]/usr/ctxtERDAT-LOW”).Text = Period
session.findById(“wnd[0]/usr/ctxtERDAT-HIGH”).Text = Period1
session.findById(“wnd[0]/usr/ctxtERDAT-HIGH”).SetFocus
session.findById(“wnd[0]/usr/ctxtERDAT-HIGH”).caretPosition = 10
session.findById(“wnd[0]/usr/btn%_KOSTL_%_APP_%-VALU_PUSH”).press
Application.DisplayAlerts = False
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
session.findById(“wnd[1]/tbar[0]/btn[8]”).press
session.findById(“wnd[0]/usr/ctxtVARIANT”).Text = “/PO WEEKLY”
session.findById(“wnd[0]/usr/ctxtVARIANT”).SetFocus
session.findById(“wnd[0]/usr/ctxtVARIANT”).caretPosition = 10
session.findById(“wnd[0]/tbar[1]/btn[8]”).press
session.findById(“wnd[1]/tbar[0]/btn[0]”).press
session.findById(“wnd[0]/mbar/menu[0]/menu[11]/menu[2]”).Select
session.findById(“wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]”).Select
session.findById(“wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]”).SetFocus
session.findById(“wnd[1]/tbar[0]/btn[0]”).press
session.findById(“wnd[1]/usr/ctxtDY_PATH”).Text = Folderpath
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).Text = “IW39.xls”
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).caretPosition = 8
session.findById(“wnd[1]/tbar[0]/btn[11]”).press
Set sht = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\CC.xlsx”).Worksheets(“Sheet1”)
LastRow = sht.Range(“A2”).CurrentRegion.Rows.Count
sht.Range(“A2:A” & LastRow).Copy
session.findById(“wnd[0]/tbar[0]/okcd”).Text = “/nme2k”
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtEK_KOSTL-LOW”).Text = “1”
session.findById(“wnd[0]/usr/ctxtEK_KOSTL-LOW”).caretPosition = 1
session.findById(“wnd[0]/usr/btn%_EK_KOSTL_%_APP_%-VALU_PUSH”).press
session.findById(“wnd[1]/tbar[0]/btn[16]”).press
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
session.findById(“wnd[1]/tbar[0]/btn[8]”).press
session.findById(“wnd[0]/usr/ctxtS_BEDAT-LOW”).Text = Period
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).Text = Period1
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).SetFocus
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).caretPosition = 10
session.findById(“wnd[0]/tbar[1]/btn[8]”).press
session.findById(“wnd[0]/tbar[1]/btn[33]”).press
Set Layout = session.findById(“wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell”)
RowsL = Layout.RowCount()
For i = 0 To RowsL – 1
LayoutVariant = Layout.getCellValue(i, “VARIANT”)
Layout.setCurrentCell i, “TEXT”
Layout.firstVisibleRow = i
Layout.selectedRows = i
If LayoutVariant = “/PO WEEKLY” Then
Layout.currentCellRow = i
Layout.clickCurrentCell
Exit For
End If
Next
session.findById(“wnd[0]/mbar/menu[0]/menu[3]/menu[1]”).Select
session.findById(“wnd[1]/usr/ctxtDY_PATH”).Text = Folderpath
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).Text = “me2kcc.xlsx”
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).caretPosition = 11
Set Ord = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\Orders.xlsx”).Worksheets(“Sheet1”)
LastRow = Ord.Range(“A2”).CurrentRegion.Rows.Count
Ord.Range(“A2:A” & LastRow).Copy
session.findById(“wnd[1]/tbar[0]/btn[11]”).press
session.findById(“wnd[0]/tbar[0]/btn[15]”).press
session.findById(“wnd[0]/usr/ctxtEK_AUFNR-LOW”).Text = “1”
session.findById(“wnd[0]/usr/ctxtEK_AUFNR-LOW”).SetFocus
session.findById(“wnd[0]/usr/ctxtEK_AUFNR-LOW”).caretPosition = 1
session.findById(“wnd[0]/usr/btn%_EK_AUFNR_%_APP_%-VALU_PUSH”).press
session.findById(“wnd[1]/tbar[0]/btn[16]”).press
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
Set IW39 = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\IW39.xls”).Worksheets(“IW39”)
SendKeys “%Y”
LastRow = IW39.Range(“B4”).CurrentRegion.Rows.Count
IW39.Range(“B4:B” & LastRow).Copy
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
session.findById(“wnd[1]/tbar[0]/btn[8]”).press
session.findById(“wnd[0]/usr/ctxtS_BEDAT-LOW”).Text = Period
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).Text = Period
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).SetFocus
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).caretPosition = 10
session.findById(“wnd[0]/tbar[1]/btn[8]”).press
session.findById(“wnd[0]/tbar[1]/btn[33]”).press
Set Layout = session.findById(“wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell”)
RowsL = Layout.RowCount()
For i = 0 To RowsL – 1
LayoutVariant = Layout.getCellValue(i, “VARIANT”)
Layout.setCurrentCell i, “TEXT”
Layout.firstVisibleRow = i
Layout.selectedRows = i
If LayoutVariant = “/PO WEEKLY” Then
Layout.currentCellRow = i
Layout.clickCurrentCell
Exit For
End If
Next
session.findById(“wnd[0]/mbar/menu[0]/menu[3]/menu[1]”).Select
session.findById(“wnd[1]/usr/ctxtDY_PATH”).Text = Folderpath
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).Text = “ME2k_PM CC.xlsx”
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).caretPosition = 15
session.findById(“wnd[1]/tbar[0]/btn[11]”).press
Set WBS = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\WBS.xlsx”).Worksheets(“Sheet1”)
LastRow = WBS.Range(“A2”).CurrentRegion.Rows.Count
WBS.Range(“A2:A” & LastRow).Copy
session.findById(“wnd[0]/tbar[0]/okcd”).Text = “/nme2j”
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[1]/usr/ctxtTCNT-PROF_DB”).Text = “000000000001”
session.findById(“wnd[1]/usr/ctxtTCNT-PROF_DB”).caretPosition = 11
session.findById(“wnd[1]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtCN_PSPNR-LOW”).SetFocus
session.findById(“wnd[0]/usr/ctxtCN_PSPNR-LOW”).caretPosition = 0
session.findById(“wnd[0]/usr/btn%_CN_PSPNR_%_APP_%-VALU_PUSH”).press
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
session.findById(“wnd[1]/tbar[0]/btn[8]”).press
session.findById(“wnd[0]/usr/ctxtS_BEDAT-LOW”).Text = Period
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).Text = Period1
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).SetFocus
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).caretPosition = 10
session.findById(“wnd[0]/tbar[1]/btn[8]”).press
session.findById(“wnd[0]/tbar[1]/btn[33]”).press
Set Layout = session.findById(“wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell”)
RowsL = Layout.RowCount()
For i = 0 To RowsL – 1
LayoutVariant = Layout.getCellValue(i, “VARIANT”)
Layout.setCurrentCell i, “TEXT”
Layout.firstVisibleRow = i
Layout.selectedRows = i
If LayoutVariant = “/PO WEEKLY” Then
Layout.currentCellRow = i
Layout.clickCurrentCell
Exit For
End If
Next
session.findById(“wnd[0]/mbar/menu[0]/menu[3]/menu[1]”).Select
session.findById(“wnd[1]/usr/ctxtDY_PATH”).Text = Folderpath
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).Text = “WBS_ME2J.xlsx”
Set List = Workbooks.Open(“C:\Users\GOYALANA\OneDrive – Novartis Pharma AG\DPS\Automation\PO weekly\Project list.xlsx”).Worksheets(“Sheet1”)
LastRow = List.Range(“A2”).CurrentRegion.Rows.Count
List.Range(“A2:A” & LastRow).Copy
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).caretPosition = 13
session.findById(“wnd[1]/tbar[0]/btn[11]”).press
session.findById(“wnd[0]/tbar[0]/btn[15]”).press
session.findById(“wnd[0]/usr/ctxtCN_PSPNR-LOW”).Text = “”
session.findById(“wnd[0]/usr/ctxtCN_PSPNR-LOW”).SetFocus
session.findById(“wnd[0]/usr/ctxtCN_PSPNR-LOW”).caretPosition = 0
session.findById(“wnd[0]/usr/btn%_CN_PROJN_%_APP_%-VALU_PUSH”).press
session.findById(“wnd[1]/tbar[0]/btn[24]”).press
session.findById(“wnd[1]/tbar[0]/btn[8]”).press
session.findById(“wnd[0]/usr/ctxtS_BEDAT-LOW”).Text = Period
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).Text = Period1
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).SetFocus
session.findById(“wnd[0]/usr/ctxtS_BEDAT-HIGH”).caretPosition = 10
session.findById(“wnd[0]/tbar[1]/btn[8]”).press
session.findById(“wnd[0]/tbar[1]/btn[33]”).press
Set Layout = session.findById(“wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell”)
RowsL = Layout.RowCount()
For i = 0 To RowsL – 1
LayoutVariant = Layout.getCellValue(i, “VARIANT”)
Layout.setCurrentCell i, “TEXT”
Layout.firstVisibleRow = i
Layout.selectedRows = i
If LayoutVariant = “/PO WEEKLY” Then
Layout.currentCellRow = i
Layout.clickCurrentCell
Exit For
End If
Next
session.findById(“wnd[0]/mbar/menu[0]/menu[3]/menu[1]”).Select
session.findById(“wnd[1]/usr/ctxtDY_PATH”).Text = Folderpath
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).Text = “Project_ME2J.xlsx”
session.findById(“wnd[1]/usr/ctxtDY_FILENAME”).caretPosition = 17
session.findById(“wnd[1]/tbar[0]/btn[11]”).press
Windows(“CC.xlsx”).Activate
ActiveWindow.Close
Windows(“WBS.xlsx”).Activate
ActiveWindow.Close
Windows(“Orders.xlsx”).Activate
ActiveWindow.Close
Windows(“Project list.xlsx”).Activate
ActiveWindow.Close
Windows(“IW39.xls”).Activate
ActiveWindow.Close
Windows(“ME2K_PM CC.xlsx”).Activate
ActiveWindow.Close
Windows(“me2kcc.xlsx”).Activate
ActiveWindow.Close
Windows(“Project_ME2J.xlsx”).Activate
ActiveWindow.Close
Windows(“WBS_ME2J.xlsx”).Activate
ActiveWindow.Close
End Sub
But can You highlight where do You struggle with the problem in your code?
Which line is the issue?
This code is fine no issues in that. This codes only log on to SAP and extract the data. I want to enhance my coding, I want to close the SAP log on as well. SO could you please give me the coding to close the SAP logon and where I can enter that coding in my code.
Thanks in advance!