Skip to Content

How to make file-dialogues scriptable (vba)

As Holger Kohn stated here, file dialogues are GUI-modal and recordable in SAP-Basis Release 740 only. Meaning, if you’re up to date, you won’t have troubles to control Save-As or Open-dialogues within your scripts. But also for those of us still on earlier releases, there’s no need to despair!

This is how you get it done:

The key to accessing external file dialogues while executing your SAPGui Script, is Windows API functions. Basically, it’s about finding the right window and bringing it to the foreground, so you can manipulate it via sendkey commands. It’s first of all these two functions which you have to make use of:

Public Declare Function FindWindow Lib “user32” _

          (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SetForegroundWindow Lib “user32” _

         (ByVal hwnd As Long) As Long

If you can’t find out the ipClassName (e.g. using tools like Spy++) and the FindWindow function won’t find and return the window handle only by it’s name, don’t worry. There is a ready-to-use solution that’s more complex but also more reliable: Access/VBA Tutorials – Bring an external application window to the foreground. (You can simply copy and paste it …)

But be careful: If you refer to Gui-object methods (e.g. .pressToolbarButton “XYZ”) to open the file-dialogue, your mission is already bound to fail: That’s because your client has to wait for the server to answer until it can proceed. Meaning, the code that follows (bringing the save-dialogue to the foreground, sendkeys etc…) will have to wait in the queue.

That’s why I am using sendkeys not only to fill-in and confirm the file-dialogue, but already to open it. This is how I got it solved, hope it will help some of you, too….

‘ ———————– Import Document ————————-


Session.findById(“wnd[0]/shellcont[0]/shell/shellcont[1]/shell”).doubleClickItem “Key11”, “Column1”
Call PressButton(“Dokument importieren”)         ‘ Finding the button turned out tricky for me, so I put it into a separate procedure, for better readability…
Application.Wait (Now + TimeSerial(0, 0, 3))      ‘ This pause makes sure the window is already open when you look for it….
bln = FnSetForegroundWindow(“Öffnen”)


SendKeys ThisWorkbook.Path & “\testmail.msg”, True
SendKeys “{ENTER}”, True


Application.Wait (Now + TimeSerial(0, 0, 1))


Session.findById(“wnd[1]/usr/cmb/WSV/CA_DOC_IMPORT-LOB”).Key = “ACC”
Session.findById(“wnd[1]/usr/ctxt/WSV/CA_DOC_IMPORT-DOCCLASS”).Text = “YOTH”
Session.findById(“wnd[1]/usr/cmb/WSV/CA_DOC_IMPORT-VISIBILITY_LEVEL”).Key = “D”


You must be Logged on to comment or reply to a post.
  • Hello,

    I am new to testing SAP. My testing tool is UFT.

    I am having an issue in getting past the Save As dialog window when trying to export to an excel file.

    I can follow most of what is shown in the code snippet above.

    In looking at the above code you are using a few functions that I did not see the code for, would yo be able to pass this info along as well?

    Functions: PressButton, FnSetForegroundWindow and  SendKeys

    I assume FnSetForegroundWindow  is wrapping the API calls of FindWindow and SetForegroundWindow

    Any more info you can provide would be a big help.


    • Hi Stephen,

      FnSetForegroundWindow is a function which encapsulates a whole routine to find the window in question and set it to the foreground. if you don’t want to reinvent the wheel,  you can simply copy and paste the code from here, Access/VBA Tutorials – Bring an external application window to the foreground

      Sendkeys is a method used to send keystrokes to the active window. In my example, I’m sending the path and filename and then, just the same way as if you would do it manually, I send Enter to save and proceed.

      PressButton is just a function I created to encapsulate various sendkeys-Commands, tabbing through I whole lot of other buttons to get to the one I need to press.

      Hope that helps!