Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

SendKeys from VBA to a session window

In context of the posting from mic jones here I check different ways of SendKey from an Excel VBA application to a session window.

At first I identfiy the handle of the session window and set it in foreground with the following code

Private Declare Function SetForegroundWindow Lib "user32.dll" _
  (ByVal hWnd As Long) As Long

hWnd = Session.ActiveWindow.Handle
SetForegroundWindow hWnd


After that I use different methods of SendKeys:

  1. Standard VBA SendKeys, but it doesn’t work.SendKeys “+{DOWN}”, True 
  2. SendKeys via SendMessage, but it doesn’t work.Private Declare Function SendMessageA Lib “user32.dll” _
      (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long

    SendMessageA hWnd, &H100, vbKeyShift, 0
    SendMessageA hWnd, &H100, vbKeyDown, 0
    SendMessageA hWnd, &H101, vbKeyDown, 0
    SendMessageA hWnd, &H101, vbKeyShift, 0
  3. SendKeys via keybd_event, but it doesn’t work.Private Declare Sub keybd_event Lib “user32.dll” _
      (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    keybd_event &H10, 0, 0, 0
    keybd_event &H28, 0, 0, 0
    keybd_event &H28, 0, 2, 0
    keybd_event &H10, 0, 2, 0 
  4. SendKeys via Windows Scripting Host and this works very fine.Dim wsh As WshShell
    Set wsh = CreateObject(“WScript.Shell”)
    wsh.SendKeys “+{DOWN}”

Maybe someone can use this information or show different ways.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo mic jones
      mic jones

      Hi Stefan

      This is what I used - for option 1 - plain old send keys. I needed a sleep. In the code - not myself!

                  Dim hwnd As Long

                  hwnd = ses.ActiveWindow.Handle

                  SetForegroundWindows hwnd

                  SendKeys "+{DOWN}", True  ' move down

                  sleep 250 ' library call

      Author's profile photo Former Member
      Former Member

      Thanks for sharing this information!  🙂

      Author's profile photo Sayuti Azmi
      Sayuti Azmi

      Dear Stefan,

      For the purpose of scrolling down a GuiTableControl object, I found using Page Down key works as well.

      We can use the GuiMainWindow.SendVKey 82 from SAP Scripting API to do this.



      Author's profile photo marcio _vr
      marcio _vr

      Dear Stefan,

      I'm having difficulty resolving an issue that seemed simple, but not being ...

      ... While trying to save a table in "RTF" format opens the window "Save As", but I'm not able to access it through the AutoIt functions, after searching here in the forum, I found a solution that uses the user32 .dll but this in VBScript. It would be possible you help me convert the code to AutoIt?

      The link this script here: Support on VB script for report

      ...posted by

      Thank you.


      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Marcio,

      you can find a solution here.