Friday, March 13, 2009

Function for Capturing Desktop Screenshot

Hi All,

Here i am providing the user defined function which will captures the screenshot of desktop & stores into required location.

' Function Defination :
Function ScreenShot()
dim strPNG
dim objDesktop
' set a unique file name using the date/time
strPNG = "C:\Screenshot_" &day(Date)&" - "&month(Date)&"-"&year(Date)&"##"& hour(time) &". "& minute(time) &". "& second(time) & ".png"

Set objDesktop=Desktop

' capture a png of the desktop
'captures the screenshot(.png img) into above specified location
objDesktop.CaptureBitmap strPNG,true

' captures the screenshot into current results folders
objDesktop.CaptureBitmap Environment("TestName")&day(Date)&" - "&month(Date)&"-"&year(Date)&".bmp",true

' destroy the object
Set objDesktop = Nothing
End Function

' Function Declaration/Calling
Call ScreenShot()

OutPut :
Screenshot of type .png will be saved in the above Loc given in Variable strPNG.
Screenshot of type .bmp will be saved into current results folders of the script.

Have a nice day.......


  1. hi,

    is there anyway to screenshoot the active window instead of the desktop? I was using your function but other windows (such as QTP itself) get in the way of the application I want to get a screenshot of.

  2. It throws me a error at the environment variable

  3. Hi jbc1,

    use "CaptureBitmap" method for the object you want...for example "window"

    Capture Bitmap:

    This method is used to capture the image from the selected object. The captured image will be save in the given path in the bitmap (.bmp) format. Below syntax will show how to insert this (Capture Bitmap) property in to the script


    Example: Scripts to capture Google window:

    Browser("Google").Capturebitmap "C:\name.bmp", True

    Browser("Google").Window("security Alert").Capturebitmap "C:\filename.bmp",True

    For more info. refer to QTP help file :-)

  4. It works perfectly.

    Many thank's