Search code examples
windowsuser-interfacemicrosoft-dynamicsautoit

Application windows flash instead of becoming active


My AutoIt script activates a Dynamics AX application based on parameters passed. I have the various AX applications open and minimized.

Most of the time the correct application (based on the $partition parameter) is activated and in focus so the script continues. But sometimes (maybe 1 out of 3) the application merely flashes in the task bar and does not activate and so the script cannot continue.

A .Net application calls my AutoIt script by parameters on Windows 2012 Server:

#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

Local $partition = $CmdLine[1]
Local $axc       = $CmdLine[2]
Local $brand     = $CmdLine[3]
Local $sTerm     = $CmdLine[4]

;command line example
;GoToCustomerServicePage.exe "msl" "MSLtd" "MSUK" "LS14 6PN"

SearchForCust($partition, $axc, $brand, $sTerm)

Func SearchForCust($partition, $axc, $brand, $sTerm)
    ;Set the Title match mode
    Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

    Local $custSer = StringUpper($partition) & "/Omnica MCR/Common/Customer Service"
    Local $exePath = "C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe \\zoom-sql2\axcshare\" & $axc & ".axc"
    Local $axTitle = "[TITLE:" & $partition & "; CLASS:AxMainFrame]"

    ; Wait 10 seconds for the window to appear.
    WinWait($axTitle, "", 10)

    ; Test if the window exists and display the results.
    If WinExists($axTitle, "") Then
    Else
        Run($exePath)
        WinWait($axTitle, "", 20)
    EndIf

    Local $hWnd = WinGetHandle($axTitle)

    WinActivate($hWnd)

    If WinActive($hWnd) Then
    Else
        WinWaitActive($hWnd, 5)
    EndIf

    ; Simulate clicking on the address bar
    Send("{F11}")

    ;Enter this into the address bar
    Send($custSer)
    Send("{ENTER}")

    ;Set the Brand
    Send($brand)
    Send("{ENTER}")

    ;send search term
    If $sTerm <> "unavailable" Then
        Send($sTerm)
        Send("{ENTER}")
    EndIf

EndFunc

Solution

  • Try this:

    ; Test if the window exists and display the results.
    If WinExists($axTitle, "") Then
        ; add here: check if the window is active - activate if not
        If Not WinActive($axTitle) Then WinActivate($axTitle)
    Else
        Run($exePath)
        WinWait($axTitle, "", 20)
    EndIf