I have been trying to learn Web Scraping using VBScript since VBA for Outlook does not work like VBA for Excel
So I am trying to display a message box to see whether or not my script is getting to specific points. I can display the first message box but not the second one. I understand that it is due to the line wscript.quit but I need that line otherwise I can't open IE.
Msgbox ("hello"),0,("title")
set Shell = createobject("wscript.shell")
Shell.Run "google.ca"
wscript.quit
DIM IE
Set IE = CreateObject("IExplorer.exe")
Msgbox ("hello"),0,("title")
All help is appreciated, Thanks.
Related
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
I searched the web and found a vb script to tile windows on the desktop. But I want to tile specific windows or apps. For example, if I open three images in Paint and one Excel file, I only want to show Paint apps tiling on the desktop.
I changed my code as suggested by Dai, however all windows are tiling.
There is another question. In my code I filtered "Untitled -Paint". However if I opened image, there would be "filename -Paint" . How can I get them?
Here is the code so far:
Option Explicit
Dim oWS, oShell
Set oShell = CreateObject("Shell.Application")
Set oWS=CreateObject("WScript.Shell")
Dim App
App="Untitled - Paint"
If oWS.AppActivate(App) then
else
WScript.Sleep 500
oWS.SendKeys "% x"
End if
oShell.TileVertically
WScript.Quit
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
This script is supposed to open both Windows shell Status and Properties dialogs of the first found network connection which is enabled or connected. However, only the Properties dialog is opened. The verb for the Status dialog is already correct, which is "Stat&us". The script was tested and will be used under Windows XP Pro SP3 32-Bit. It was tested with a connected 3G dialup and a LAN Loopback. Both have the same problem.
dim a,b,c
set a=createobject("shell.application")
set b=a.namespace(0).parsename("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").getfolder
for i=0 to (b.items.count-1)
set c=b.items.item(i)
for j=0 to (c.verbs.count-1)
'only process connected/enabled
if (lcase(c.verbs.item(j)) = "disc&onnect") or (lcase(c.verbs.item(j)) = "disa&ble") then
'open status and properties dialogs
c.invokeverb("Stat&us") 'this doesn't work
c.invokeverb("P&roperties") 'this one works
msgbox "Press OK to close all and exit"
wscript.quit
end if
next
next
In Windows XP there is a bug whose effect requires the Status verb to be invoked from within the Explorer process. Since WScript/CScript is not a child of the Explorer process, any attempt to invoke the status verb with prove futile despite the lack of any apparent errors. This bug appears to have been fixed in later versions as the script below is tested and working in Vista x64.
Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder
For Each objShellFolderItem in objShellFolder.Items
Set colShellFolderItemVerbs = objShellFolderItem.Verbs
For Each objShellFolderItemVerb in colShellFolderItemVerbs
strVerb = objShellFolderItemVerb.Name
If (strVerb = "C&onnect / Disconnect") Then
objShellFolderItem.InvokeVerb("Properties")
objShellFolderItem.InvokeVerb("Status")
MsgBox "Press OK to close and exit"
WScript.Quit(0)
End If
Next
Next
Option 1
Does that mean that you're out of luck? Not entirely. I have two different suggestions for you. The first uses a little trickery. Status is the default action for any network connection while it is in a connected state. Open up your network connections, right-click the connection your wish to monitor and choose Create Shortcut. You can place the shortcut anywhere you like. By default it will be named something like "Wireless Network Connection - Shortcut.lnk" on your Desktop. Typing that on the command line or via the Run or Exec methods in your script will do exactly what you need. I tried playing around with doing this all via scripting but ran into issues tryint to automate the Create Shortcut verb.
Option 2
A second option is also a bit of a workaround but may work if your 3G connection uses the dialup networking. The command line rundll32.exe rnaui.dll,RnaDial {name of connection to establish} will open the dialog to connect, however, if already connected, it opens the Status dialog for the connection. You could then try a script like this:
Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder
For Each objShellFolderItem in objShellFolder.Items
strConnection = objShellFolderItem.Name
strCommandLine = "rundll32.exe rnaui.dll,RnaDial " & Chr(34) & strConnection & Chr(34)
Set colShellFolderItemVerbs = objShellFolderItem.Verbs
For Each objShellFolderItemVerb in colShellFolderItemVerbs
strVerb = objShellFolderItemVerb.Name
If (strVerb = "C&onnect / Disconnect") Then
objShellFolderItem.InvokeVerb("Properties")
CreateObject("WScript.Shell").Run strCommandLine
MsgBox "Press OK to close and exit"
WScript.Quit(0)
End If
Next
Next
Option 3
A final option would be to use WMI to display the information about your network connection. This is a more traditional scripting approach.
In any case, I hope this helps out. Don't forget to change the verbs as required. They do change from one version of Windows to the next.
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?