How to set focus on file dialog opened in Vbscript - windows

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName

My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.

Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Related

Wscript does not run .vbs script due to an excel message box which pops up during refresh process

Task: I am currently working on an automation task via Windows Task Scheduler, so users can have a daily / weekly report.
Issue: I am running .vbs script, from a .bat file. The macro in the .vbs script refreshes all the pivot tables / slicers within the report and saves updated report, then it closes the Excel report application.
During testing I noticed that the .bat file stops at the point of running the refresh because Excel report comes up with the below msgBox message and then requires YES/NO in order to continue thereafter that another msg box asking to click YEs to replace the current file after refresh.
Below are the two msgbox that comes up in Excel:
Question: How do I write the .vbs script to automatically click yes to the msg boxes instead of doing it manually, there is no point of automation if there is any manual work needed. I need a clean smooth automation and I have tried to write the .vbs script in different ways.
the .vbs script below:
'Input Excel File's Full Path
'ExcelFilePath = ***Excel report file path
'Input Module/Macro name within the Excel File
MacroPath = "RefreshReport"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False 'or "True"
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True 'or "False"
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
'MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
Have you tried sending {Enter} in the script to accept the MsgBox?
set shell = createobject("wscript.shell")
shell.sendkeys ("{Enter}")
https://ss64.com/vb/sendkeys.html

Using VBS script SendKeys to activate hotkeys through Task Scheduler

I have Open Broadcaster Software installed and I want to use it to automatically record the desktop at scheduled times. In OBS, I've set up a "start record" hotkey (in this cast ALT 1) and a "stop record" hotkey (ALT 2).
OBS starts with Windows and opens minimized to the task bar.
Pressing the hotkeys keys successfully starts and stops screen recordings regardless of what program is open or focused on.
I want to be able to use Task Scheduler to schedule recordings at specific times throughout the week. Task Scheduler cannot schedule key strokes so I created a start.vbs file and a stop.vbs file. Here's where my problem is, I can't figure out how to get the 'ALT 1' or 'ALT 2' keys to be recognized by OBS.
Here's what I have for my start.vbs:
'Start Recording
Option Explicit
Dim WSHShell, strDesktop
Set WSHShell = WScript.CreateObject("WScript.Shell")
strDesktop = WSHShell.SpecialFolders("Desktop")
WSHShell.AppActivate strDesktop
WSHShell.SendKeys "%1"
WScript.Quit
It seems to run fine, but the keystrokes don't register. Am I doing something wrong, or is there a better way to go about this?
Thanks

Can an already running program's window be hidden?

I have been using wsh to run vbscripts in conjunction with iMacros at my work for going on 3 years now. I know that the iMacros browser itself has an option to run hidden but the web-based db interface I have to work with does not function in the iMacros browser. For this reason I have had to use Internet Explorer. I have no problems with the script itself or how it runs. The thing I would like to improve is how the Internet Explorer window itself is handled.
Currently, when iMacros is initiated it creates the IE window in a non-maximized state cascaded from where the previous window was created. Because of iMacros's behavior, I then resize the window using an iMacros script so that enough of the page is rendered for the script to see everything it needs.
Set iim1 = CreateObject ("imacros")
iret = iim1.iimOpen ("-ie -iePrivate", TRUE, 300)
iret = iim1.iimPlayCode("SIZE X=" & scrWidth & " Y=" & scrHeight)
This all works fine and dandy. The scripts are scheduled to run at specific times, gather information out of the db and then imports that data into Excel spreadsheets and prints them out.
What I would like to do is make the IE window hidden while these scripts run. Because I do not create the IE window first (Set objIE = CreateObject("internetexplorer.Application")) I do not have access to the IE window object. I let iMacros create the window with the -iePrivate flag so that it will not disturb my own IE window if I should be logged in and working in the db while the script executes. However, it does try to take focus and become the active application while the script executes. Which can be very annoying at time. My goal is to be able to share these scripts with my co-workers but I don't want the IE windows popping up on them while they may be working on something else.
Is there a way to get to the IE object created by iMacros so that the window can be hidden?
This does Internet Explorer and Explorer windows (they used to be the same program).
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.locationname
Next

Need a startup script in windows to send ALT+F11 keys?

We are using a shared desktop on thin clients. By default it comes with windowed screen, but by pressing the Alt + F11 keys it will restore to full screen. So, we want a script to execute at Windows log-on with some delay.
Hopefully a login vbscript will work for you. Either append this to an existing login script or save it as a ".vbs" file. Microsoft has some good tutorials if you are unfamiliar with login scripts.
Set WshShell = CreateObject("Wscript.Shell") 'Create wshell object
WScript.Sleep(5000) 'Lets wait 5 seconds
WshShell.AppActivate "EXACT TITLE OF THE WINDOW YOU WANT" 'EDIT THIS LINE!
'The line above selects the window so we make sure the keystrokes are sent to it
WshShell.SendKeys "%{F11}" 'Send ALT+F11
Wscript.Quit 'Quit the script

Minimize windows, application startup

Is there a way to minimize all windows automatically when launching an application?
I tried to call a .vbs file with the content above:
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll
But doing this, application is minimized too.
I'm not sure if this minimizes the windows, but it will probably satisfy your requirement:
set objShell = CreateObject("shell.application")
objShell.ToggleDesktop
This is equivalent to clicking the show desktop button.
Or as just extending your code:
shell.ToggleDesktop
Edit: Nevermind... this does the same thing as your code above. But why not just run this and then display your window? For instance, if it is your app... call the vbs and then display the window. Or if you have no control over the app, call it from a .bat file and run this vbs then your program.
What you want to do is first minimize all applications or toggle the desktop; THEN (perhaps even after a delay) open your application.

Resources