How to close SAP Logon window?

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")
    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")
    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:
and 1 constant:

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.


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.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

Leave a Reply

Your email address will not be published. Required fields are marked *