Wednesday, 30 March 2016

VBScript to capture a screenshot and save as a jpg




Despite searching online for a VBScript that captures a screenshot and saves to a jpeg, all I found were incomplete answers.

You'd think such a common request would have many answers - how hard can it be to do a screen capture and save with VBScript, a scripting language that's been around forever? Every time a QTP script runs you'd think the answer would be in use.

Well, maybe it is at least in some form or other. What I didn't find was a clear answer, a working example on any forum or a solid code snippet proven to have worked. I did find the question being asked way back in 2005, no clear answer in 10 years? Forget that. Look no further, here's the script!

But first... a caveat or two
As with all things VBScript, it's great to have the facility to do automation using it (on a Windows system) but it's often a little unpredictable. I've found that this script works on some machines but not othersIt's possible to assess what version of Windows OS you have and the patch level, the WIndows Script Host version, browser, etc. but despite this I can't see a correlation.

Exactly why this is I've not been able to work out, feel free to leave your own research and comments!

The 2nd caveat is for this script you'll need Microsoft Word installed. I'm sure you can find a way around this but as most business Windows systems have office I've gone for using it.

OK, here's the script in it's complete form:


OptionExplicit  
' ------- Declare the variables  -----------------  
Dim oIE, WshShell  
    
' ------- Wait until the webpage is loaded  --------------  
Sub WaitForLoad  
                 DoWhile oIE.Busy        
                   WScript.Sleep 500  
                 Loop  
EndSub  
' ------- Blocks of code for the test steps  -------------  
Sub OpenPaint  
                 Set WshShell = WScript.CreateObject("WScript.Shell")  
                 WshShell.Run "mspaint"  
                 WScript.Sleep 5000  
EndSub  
    
Sub OpenIEAndGoToGoogle  
                 Set oIE = CreateObject("InternetExplorer.Application")  
                 oIE.Visible = True  
                 oIE.Navigate "https://www.google.co.uk"  
                 Call WaitForLoad  
EndSub  
    
Sub ActivateIE  
                 WshShell.AppActivate "Google - Internet Explorer"
                 WScript.Sleep 1000  
EndSub  
    
Sub TakeScreenShot  
                 Set Wshshell = CreateObject("Word.Basic")  
                 WshShell.SendKeys "(%{1068})"'Screenshots the currently active window, not the whole screen  
                 WScript.Sleep 1000  
EndSub  
    
Sub ActivatePaintAndSaveTheImage  
                 WshShell.AppActivate "Untitled - Paint"  
                 WScript.Sleep 1500  
    
                 WshShell.sendkeys "^(v)"  
                 WScript.Sleep 1500  
    
                 WshShell.sendkeys "^(s)"  
                 WScript.Sleep 1500  
    
                 WshShell.sendkeys "testing.jpg"  
                 WScript.Sleep 1500  
    
                 WshShell.sendkeys "%(s)"  
                 WScript.Sleep 1500  
EndSub  
    
Sub ClosePaintAndIE  
                 WshShell.AppClose "Paint"  
                 WScript.Sleep 1500  
                 WshShell.AppClose "Google - Internet Explorer"  
                 WScript.Sleep 1500  
EndSub  
    
' ------- Call the Blocks of code  ----------------  
Call OpenPaint  
Call OpenIEAndGoToGoogle  
Call ActivateIE 
Call TakeScreenShot  
Call ActivatePaintAndSaveTheImage  
Call ClosePaintAndIE



As you'll see, this is split into a series of subs for testing purposes. It's easier to control the flow of execution and see the blocks of code. Of course you could just have it as a single block of code to run.

The assumption above is that you have IE open in Google, but you can navigate where ever you want to go.

A critical point -  the app's (e.g. IE or Paint) window will NOT be found unless you get that name absolutely correct. A way to do this is to [ctrl] + [alt] + [del] then select task manager or hit [Win key] + [r] and type 'taskmgr' - then look carefully at the window/app name.

Hope the above works for you!

Look out for the next post where we'll use this and the Include method we discussed in another post, to create a simple Automation Framework with VBScript.

Mark.

------------------------------------------------------------------
Enjoyed this post?
Say thanks by sharing, clicking an advert or checking out the links above!
Costs you nothing, means a lot.
------------------------------------------------------------------




0 comments: