Saving snapshot using vbscript

codeomnitrix picture codeomnitrix · Apr 9, 2012 · Viewed 17.6k times · Source

I am new to vbscript. I want to save a snapshot taken using vbscript of a internet explorer window opened by vbscript.

Code to load the page

Dim IE, stateString
Set IE = WScript.CreateObject("InternetExplorer.Application")
ie.toolbar = 1
ie.statusbar = 1
ie.width = 999
ie.height = 500
ie.left = 20
ie.theatermode = false
ie.theatermode = true
ie.theatermode = false
ie.top = 50
ie.navigate("file:///C:\Users\Vinit_Tiwari\Documents\vbscripts\someform.html")
'stateString = cstr(ie.readystate)
waitforload(ie)


ie.visible = 1

Set wshShell = CreateObject("Wscript.Shell")

wshShell.AppActivate "Some Form"
wshShell.SendKeys "% x"

Code to take snapshot

Dim oWordBasic
set oWordBasic = CreateObject("Word.Basic")
oWordBasic.sendkeys "{prtsc}"
oWordBasic.AppClose "Microsoft Word"
Set oWordBasic = Nothing
Wscript.Sleep 2000

Saving the snapshot

dim paint
set paint = wshShell.exec("mspaint")
do while paint.status = 0:loop
wshShell.appactivate("untitled-Paint")'this returns false
Wscript.sleep 500
WshShell.SendKeys "^v"
wscript.sleep 500
wshshell.sendkeys "^s"
wscript.sleep 500
wshshell.sendkeys "d:\test.png"
wscript.sleep 500
wshell.sendkeys "{Enter}"
Set wshshell = Nothing

Actually the previously opened ie window is having focus and the keystrokes are sent to the ie instead of paint. so is there any function which performs opposite work of AppActivate.

Answer

Nilpo picture Nilpo · Apr 9, 2012

This won't work. Microsoft Word is in focus while the Print Screen button is pressed. You can easily eliminate this problem by not using Microsoft Word at all. It's not necessary. The Print Screen function is a system hotkey and has nothing to do with Microsoft Word. You should be using the Alt+PrintScreen combination which captures a screenshot of the currently focused window to the clipboard.

Second, if Paint is not in focus instead of IE, it's because you have the window title wrong. You are doing that part correctly. AppActivate returns false if it cannot find a window with the specified title. To be honest, Paint should alread have focus to begin with but it is good practice to make sure the window is activated first.

Also, is this a very old system? Why are you using the Word.Basic automation object anyway out of curiosity?