Quantcast
Channel: Scripting Languages
Viewing all articles
Browse latest Browse all 42

Control SaveAs Dialog with AutoIt

$
0
0

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


Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>