0

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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Trm
  • 479
  • 2
  • 9
  • 22

1 Answers1

0

Just put an AppActivate before every sendkeys, but not in a loop. That's the best and only option.

But why not control SAP direct from VBS. See http://scn.sap.com/community/gui/blog/2012/10/08/introduction-to-sap-gui-scripting and look for things similar to objSAP.Alerts = False (depends on why you are getting the dialog).

Also why do you get the dialog? Is it because the file already exists? If so delete it and no dialog.

Now I said it's the best and only except you can use API calls in vbscript by spinning out a vb.net helper program. This links to two articles I've written on how to do that. If you go that path look at GetActiveWindow API call (among others) How To run vb.net with vbs scheduled task.

Try two things

Appactivate "Save PDF File As"
WScr.SendKeys FileName          
WScript.Sleep 250           
Appactivate "Save PDF File As"
WScr.SendKeys "{ENTER}"         
Appactivate "Save PDF File As"
WScr.SendKeys "{TAB}" ' in case same name exists, overwrite question tab chooses yes            
Appactivate "Save PDF File As"
WScr.SendKeys "{ENTER}" ' press yes
WScript.Sleep 500                   

The second is replace the File Save dialog title with the Top Level Window (it is also the dialog's owner window) title - ie the main window title. If Notepad's Save As dialog is open use AppActivate "Untitled - Notepad"

Community
  • 1
  • 1
Noodles
  • 1,981
  • 1
  • 11
  • 4
  • Hi, thanks for the answer, however i was not able to make it work with adding appactive with sendkeys. Then commands are not recognized and nothing happens. Regarding SAP GUI, I already do adjustments directly in SAP. After they are done, changes are saved and SAP generates spool request which then sends to default printer. It launches SAPLPD transfer program to generate printout. To save pdf file into the disk with the order name, I would need to change code within SAP and not even sure I can do it. It is also not available for end users and it is more high end solution. – Trm Aug 06 '14 at 13:42
  • just to be clear. I have a lot of orders with input that needs to be changed which is entered in excel. Then script goes in the system, does the changes. As mentioned, printout is like another application launching and i can't set settings in SAP to do it for me (not as an end user anyway). that is why current solution is to detect save as dialog and save. Same name can exist if printout was done and more adjustments are done in the same order and printed again. Can you elaborate about getActiveWindow API for my case? I saw it in some comments, but couldnt apply for my problem – Trm Aug 06 '14 at 13:48
  • We don't really know why it's not working as is yet. It would usually work for most applications (unless your keystrokes are wrong?). I've added to my answer. – Noodles Aug 06 '14 at 13:58
  • thanks alot. I have understood it wrong at first. I will do some testing and let you know if I get same issue. I can't replicate it, so I need to try it for a period and see if it happens again. second solution looks interesting too. I will try to work on it. It looks like more error proof as I would imagine it could not work if someone is using local language settings so name wont be found – Trm Aug 06 '14 at 14:16
  • Hi again. After some testing problem seems to still persist even with appactivate before each command. Problem looks only to happen after fresh bootup and at first printout. Same thing persists with other colleagues computers so most likely printer popup window does not work correctly and not the issue with the code – Trm Aug 12 '14 at 10:59