Saving snapshot using vbscript - vbscript

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.

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?

Related

Set keyboard combination for .lnk shortcut [duplicate]

This question already has an answer here:
How to write VBScript to start a shortcut in a given folder?
(1 answer)
Closed 9 months ago.
I am trying to set keyboard combination for a .lnk shortcut using vbs. For some reason the 'shortcut key' section will not accept emulated letter key presses. All I get is pause key. For some reason Enter key as well as Tab, Esc work, meaning I can switch to next line or close shortcut properties altogether. Please adivise.
This is the VBS script I am using.
set WshSHell = Wscript.CreateObject("WScript.Shell")
WshShell.SendKeys "{1}" *for simplicity I named the shortcut "1"*
WshShell.SendKeys "+{F10}" *emulates righ-click*
WshShell.SendKeys "{UP}" *select 'Properties'*
WshShell.Sendkeys"{ENTER}"
WScript.Sleep 500 *wait fo window to pop-up*
WshShell.SendKeys "{TAB 2}" *switch to 'shortcut key' line*
WshShell.SendKeys "{G}" *error*
WshShell.SendKeys "{ENTER}"
There's no need to use Sendkeys to do this. You can actually edit the properties of the shortcut, including the Hotkey in code.
From the Microsoft Documentation
WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Sample.lnk")
oMyShortcut.WindowStyle = 3 &&Maximized 7=Minimized 4=Normal
oMyShortcut.IconLocation = "C:\myicon.ico"
OMyShortcut.TargetPath = "%windir%\notepad.exe"
oMyShortCut.Hotkey = "ALT+CTRL+F"
oMyShortCut.Save
I did some tweaking and it now suits my needs. Most of all I had to place 'set' at the begining of WshShell and first oMyDesktop lines and remove what seems to be a comment on 'WindiwStyle' line. Please see below.
set WshShell = CreateObject("Wscript.shell")
set oMyShortcut = WshShell.CreateShortcut("PATH\test.lnk")
oMyShortcut.WindowStyle = 4
oMyShortcut.IconLocation = "%SystemRoot%\System32\imageres.dll,63"
OMyShortcut.TargetPath = "PATH\test.bat"
oMyShortCut.Hotkey = "ALT+CTRL+K"
oMyShortCut.Save

How to Maximize Screen from VBScript?

The web application that displays database information is much more readable in full-screen mode rather than a small window. Problem is, I cannot get the window to maximize from UFT/QTP.
I've tried running the browser object from Wscript.Shell, but the application returns to UFT and maximizes that window instead of the newly created browser window.
siteA = "https://google.com"
Const max_window = 3
Set browobj = CreateObject("Wscript.Shell")
Set oShell = CreateObject("WScript.Shell")
'browobj.Run "chrome -k -incognito -url "&siteA
browobj.Run "chrome -incognito -url "&siteA, max_window
oShell.SendKeys "% x"
browobj.sendkeys "{F9}"
browobj.sendkeys "(% )X"
browobj.SendKeys "% x"
Set browobj = Nothing
Any solutions to maximizing the window with the focus object of the new browser?
Edit:
Even below will not maximize taken from this List of Controls is not working.
SystemUtil.Run "chrome.exe" , siteA ,,,3
chrome.exe --incognito --start-maximized
If you shut down all instances of chrome these controls work fine, but if you have an active window in chrome it will take those properties.
We have a function defined during our browser login process that grabs the window and maximises it so that our UFT processes run with a (nearly) full screen browser - we don't need to hide the menu bar etc. This is the function we are using:
Public Function MISC_MaximiseBrowser(ByVal oBrowser, ByRef ErrorMsg)
LOG_Write vbNewLine & "MISC_MaximiseBrowser"
LOG_Write "oBrowser: " & oBrowser.GetROProperty("TestObjectName")
Dim Hwnd
dim bIsMax, bIsMaxable
MISC_MaximiseBrowser = True
Hwnd = oBrowser.Object.HWND
If Window("hwnd:=" & Hwnd).GetROProperty("maximized") Then
bIsMax = True
Else
bIsMax = False
End If
If Window("hwnd:=" & Hwnd).GetROProperty("maximizable") Then
bIsMaxable = True
Else
bIsMaxable = False
End If
If Not bIsMax And bIsMaxable Then
Window("hwnd:=" & Hwnd).Maximize
End If
End Function
What this does is accepts the Browser UFT object you're working with and grabs the handle for it. If the browser is not already maximized and it's possible to maximize it, it will do so.
You can ignore the LOG_Write statements at the start of the function - those are for our internal logging steps

How to left click in VBScript

I have a problem trying to do a click with SendKeys in VBScript.
I have this code:
Set Click = CreateObject("WScript.Shell")
WScript.Sleep 7000
Click.SendKeys ("{CLICK LEFT, 1165, 623}")
WScript.Sleep 300
WshShell.SendKeys ("{ENTER}")
I have tried also with: Click.SendKeys "{CLICK LEFT, 1165, 623}" but doesn´t work
This is the error that appears when the script arrives to the click (line 74):

.vbs facebook program not entering username and password

I am trying to create a program that asks the user their username and password for facebook in two seperate input boxes and then opens internet explorer and then goes to facebook.com and enters the username and password and logs in. Well, when I run the code you see down in the bottom it dosen't work because for some reason internet explorer is not selected when it opens so I have to click on it, and it never enters the username or password!
CODE:
Option Explicit
Dim ie, x
Set ie = createobject("InternetExplorer.Application")
Set x = createobject("wscript.shell")
Sub Load
Do while ie.Busy
wscript.sleep 200
Loop
End Sub
ie.Navigate "https://www.facebook.com/"
ie.Toolbar=0
ie.StatusBar=0
ie.Resizable=1
ie.Visible=1
Call Load
x.sendkeys "cow"
x.sendkeys "{tab}"
x.sendkeys "pass"
Try this:
Option Explicit
Dim ie, x
Set ie = createobject("InternetExplorer.Application")
Set x = createobject("wscript.shell")
ie.Visible=1
ie.Toolbar=0
ie.StatusBar=0
ie.Resizable=1
Sub Load
Do while ie.Busy
wscript.sleep 100
Loop
End Sub
x.AppActivate "Microsoft Internet Explorer"
ie.Navigate "https://www.facebook.com/"
Call Load
x.sendkeys "cow"
x.sendkeys "{tab}"
x.sendkeys "pass"
The main take-aways are -
Make the window visible before navigating to facebook. (Or atleast before AppActivate)
Use AppActivate to select the Window before passing id/password.

Clicking on a link that contains a certain string in VBS

I'm trying to run an automated vbs script that clicks on a link on a page. I have things of the form:
Const READYSTATE_COMPLETE = 4
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.Visible = true
IE.navigate ("http://mywebpage.com")
How do I then make it click on a link on that page that doesn't have an ID but is like
ClickMe!
Thanks!
Along the lines of
Dim LinkHref
Dim a
LinkHref = "link"
For Each a In IE.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
Instead of LCase(…) = LCase(…) you could also use StrComp(…, …, vbTextCompare) (see StrComp() on the MSDN).

Resources