Skip to Content
Author's profile photo Stefan Schnell

Control SaveAs Dialog with AutoIt

Hello community,

a few times we discuss here the necessity to control Windows dialogs e.g. like SaveAs in the context of SAP GUI Scripting. For those dialogs offers SAP GUI Scripting no possibilities to control that. So it is necessary to control it by ourself by indirect means. Holger Köhn presented a VBA solution here. Here now an equivalent solution for AutoIt.

A few interesting findings:

  • FindWindowEx works not in any case, it is possible to use EnumChildWindows function.
  • The Save button from the SaveAs dialog delivers in the German version the text Open (Ö&ffnen) – weird.
  • It seems that the dialogs are version and language dependent.

The code shows different access methods to find a handle of a control resp. window – FindWindow, FindWindowEx and EnumChildWindows – with AutoIt. I thinks this code is a good base to implement an individual solution for your use case.

;-Begin-----------------------------------------------------------------

  ;-Directives----------------------------------------------------------
    AutoItSetOption ("MustDeclareVars" , 1)

  ;-Includes------------------------------------------------------------
    #Include "C:\Language\AutoIt\Include\SendMessage.au3"
    #Include "C:\Language\AutoIt\Include\WinAPI.au3"
    #Include "C:\Language\AutoIt\Include\WinAPISys.au3"
    #Include "C:\Language\AutoIt\Include\WindowsConstants.au3"
    #Include "C:\Language\AutoIt\Include\MsgBoxConstants.au3"
    #Include "C:\Language\AutoIt\Include\GuiButton.au3"
    #Include "C:\Language\AutoIt\Include\Array.au3"
    #Include "C:\Language\AutoIt\Include\GuiEdit.au3"

  ;-Function _WinAPI_FindWindowEx---------------------------------------
    Func _WinAPI_FindWindowEx($hWndParent, $hWndChildAfter, _
      $sClassName, $sWindowName)
      Local $aResult = DllCall("user32.dll", "hwnd", "FindWindowExW", _
        "hWnd", $hWndParent, "hWnd", $hWndChildAfter, _
        "wstr", $sClassName, "wstr", $sWindowName)
      If @error Then Return SetError(@error, @extended, 0)
      Return $aResult[0]
    EndFunc

  ;-Sub Auto_SaveAs_SAP-------------------------------------------------
  ;
  ; Function checked with SAP 7.31 SP 4 and Dialog Box in SE80 to save
  ; source code as local file on a German version of Windows 7 with
  ; AutoIt 3.3.14.2
  ;
  ; Important hint: FindWindowEx works not in any case, so it is
  ;                 possible to use instead EnumChildWindows
  ;
  ;---------------------------------------------------------------------
    Func Auto_SaveAs_SAP($FileName)

      ;-Variables-------------------------------------------------------
        Local $i

      ;Get the handle of the Save As Dialog Box
      Local $hWin = _WinAPI_FindWindow ("#32770", "Speichern unter")
      If $hWin = 0 Then
        MsgBox($MB_OK, "", "Save As Window Not Found")
        Return
      EndIf

      ;Get the handle of ComboBoxEx32
      Local $hChildRet = _WinAPI_FindWindowEx($hWin, 0, "ComboBoxEx32", "")
      If $hChildRet = 0 Then
        MsgBox($MB_OK, "", "ComboBoxEx32 Not Found")
        Return
      EndIf

      ;Get the handle of the Main ComboBox
      $hChildRet = _WinAPI_FindWindowEx($hChildRet, 0, "ComboBox", "")
      If $hChildRet = 0 Then
        MsgBox($MB_OK, "", "ComboBox Window Not Found")
        Return
      EndIf

      ;Get the handle of the Edit
      Local $hChildArray = _WinAPI_EnumChildWindows($hChildRet)
      $hChildRet = $hChildArray[1][0]
      If $hChildRet = 0 Then
        MsgBox($MB_OK, "", "Edit Window Not Found")
        Return
      EndIf

      _WinAPI_SetForegroundWindow($hWin)
      _WinAPI_BringWindowToTop($hWin)

      ;fillin FileName in 'Save As' Edit
      _GUICtrlEdit_SetText($hChildRet, $FileName)

      ;Get the handle of the Save Button in the Save As Dialog Box
      $hChildArray = _WinAPI_EnumChildWindows($hWin)
      For $i = 0 To UBound($hChildArray, $UBOUND_ROWS) - 1
        If $hChildArray[$i][1] = "Button" Then
         ;The Save Button gets as Window Text Open - Crazy
          If _GUICtrlButton_GetText($hChildArray[$i][0]) = "Ö&ffnen" Then
            $hChildRet = $hChildArray[$i][0]
          EndIf
        EndIf
      Next

      ;Check if we found it or not
      If $hChildRet = 0 Then
        MsgBox($MB_OK, "", "Save Button in Save As Window Not Found")
        Return
      EndIf

      ;Press Save-button
      _SendMessage($hChildRet, $BM_CLICK, 0, 0)

    EndFunc

  ;-Sub Main------------------------------------------------------------
    Func Main()
      Auto_SaveAs_SAP("Test.txt")
    EndFunc

  ;-Main----------------------------------------------------------------
    Main()

;-End-------------------------------------------------------------------

Hint: You can find here WinSpy++, a phantastic freeware tool analyze the UI, controls, handles and many more.

Enjoy it.

Cheers
Stefan

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Márcio Vieira
      Márcio Vieira

      Dear Stefan!

      Once again I would like to thank the speed and efficiency of all time, it is human beings like you that the world needs, thank you for help and teach us more and more every day.

      The script worked perfectly here!

      Thank you!

      Márcio.

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

      Hello Marcio,

      thank you very much for your reply.

      🙂

      Cheers

      Stefan

      Author's profile photo Camilo Maiz
      Camilo Maiz

      Dear Stefan,

      I have a TRX in SAP (n1PATORG) with a Save as Pop up, and i cant identificate the Main Combobox with your code.

      I identificate the save as dialog box succesfully, and the bombobox32 (in my sap its called toolbarwindow32)

      I used autoit help to identificate the very items in the window, and i dont know how to proceed.

      Even i identificate the save button.

      The main of my code its simple put a name to a file and save, nothing more.

      i hope you can help me.

      Any data yopu need to help me please ask me.

      Sorry for my English, is not my native language 🙂

      I add a image of the save as dialog box.

      Thank you very much!

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

      Camilo Maiz

      Hello Camilo,
      if the code above don't work you can use alternatively the send command, which simulates keystrokes to the active window. E.g. the name of the file, two times tab and then the enter key.

      Send("myFileName.pdf")
      Send("{TAB}{TAB}{ENTER}")

      The first send fills the filename into the field and the second send jumps with two tab to the save button and press it via enter key.

      Best regards
      Stefan