I've been searching the web for solution on how to have window focus in AppActive, but was not able to find what I need, or at least understand. In short, my code does changes in SAP system and then saves, which then later generates pdf via default printer. "A save as" popup appears at random time, which I find by AppActive and continue saving by sending keys.
Now usually popup is always on top and it is no problem, however, very rarely it appears and does not have focus, but rather blinks in the taskbar. Code then finds it and continues to put a filename anyway. Since it does not have focus it puts name in whatever is in focus (usually SAP).
How can I make sure that window is always in focus? I am very new at coding so I like simple solutions :).
Set WScr = CreateObject("WScript.Shell")
Do
WScript.Sleep 50
Loop Until WScr.AppActivate("Save PDF File As") ' loops until save as popup appears
WScript.Sleep 1000
FileName = filepath & order(i) & ".pdf"
WScr.SendKeys FileName
WScript.Sleep 250
WScr.SendKeys "{ENTER}"
WScr.SendKeys "{TAB}" ' in case same name exists, overwrite question tab chooses yes
WScr.SendKeys "{ENTER}" ' press yes
WScript.Sleep 500
Set WScr=Nothing