Operate a Desktop program with sofware - vbscript

I would like to operate a (Windows) Desktop program. Because i use this on a daily basis to do some simple operaition. (Open a file PDF file, render it and save it in another folder).
I would like to do this by using somekind of service or script. I have been looking into VBscript to do this but i'm getting stuck at some operations that are normally done by users.
For example if i would like to open a file a user would go to File > Open and select the desired file. Off course the script could go to file and open but how do you select a certain file (the newest file in a certain folder).
Is it possible to have a piece of code to do these operations within a Desktop program or am i trying to do something impossible or maybe i'm using the wrong tools. Below a piece off testcode for what i'm trying to accomplish.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files (x86)\programname.exe"""
wsh.sleep 1000 ' wait 1 second
WshShell.AppActivate """Program name"""
WshShell.SendKeys "^o" ' Open file
Any help is greatly appreaciated.

You are trying to control an application through its GUI, but it is not designed for that.
Having said that, I estimate this will be a nightmare if you do not use tools (or an app :-) ) explicitely created for that purpose, a la test robots like HP QTP or automation tools like Autoit http://www.autoitscript.com/site/autoit.
For example, if your application occasionally flushes the keyboard buffer, you might need to wait for some GUI event before sending the keystroke. This kind of synchronization is not accomplishable using vbscript only.

Related

How to close an error message from an .exe called from a Excel-VBA script?

When calling a external .exe file from vba, like this:
Sub RunEXE()
Dim wsh As Object
SetCurrentDirectory "\\path\" 'Set Directory
Set wsh = VBA.CreateObject("program.exe ""\\path\argument""", WindowStyle:=1, waitonreturn:=True)
Is it possible to close error windows directly from VBA?
The code calls the program and correctly runs the .exe and produces all of the data that is useful to me, but to get the data back to my sheet I have to okay this window (since waitonreturn=true):
The violation appears to be something I can't impact from my VBA code, but it is not impacting the section of the .exe I need in order to do the calculations required by my code. Instead of delving into the world of memory access violation of an external piece of software, I am hoping to side step the problem by okaying it direct from the VBA code.
If I click okay on that error message everything is perfect as far as my code is concerned. I don't need the .exe to save the files causing the error, and I already have the data I need.
Is it possible to dismiss an error window from external software from my excel-VBA code?
NOTE: The error comes from the .exe program, not from excel itself.
This is a simple example of sendkeys, which will open notepad and then send an Enter key:
Sub SendEnterToNotepad()
With Application
Shell "Notepad.exe", 3
SendKeys "{ENTER}"
End With
End Sub

Command-line to show a file's properties?

I want to write an app that displays the default Windows Properties sheet, I can't find much info on doing it the normal programmatic way, so I'm thinking maybe shell out to a command-line call; does anyone know the Windows command-line to call up a file's property/details window? Not having any luck searching for it.
This window here:
In a general sense, there are two methods for going about this. How you specifically accomplish it (i.e. PowerShell, C#, etc) is up to you.
Call the ShellExecute function on the file with the PROPERTIES verb.
Call the SHObjectProperties function on the file.
Examples of both can be found here in AutoIt: http://www.autoitscript.com/forum/topic/118673-open-a-files-properties-window/
It appears that there IS no direct command-line call that can do this, so I whipped up a quick little VBScript to do it, since it appeared to be the quickest and easiest way to achieve the end result (at least for my specific need).
The VBScript simply sends the Windows Shortcut Alt + Enter; so, when a file is highlighted it'll automatically use that file as the one to send the shortcut to.
For anyone else who may want to do the same, just paste the following into a new text file and save it as a .vbs file:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%{ENTER}"
For anyone interested in an even better solution, I ended up writing a small command-line app in VB.NET to do this, here's a link to it's full info + the source code: https://geekdrop.com/x/props

How to comfortably monitor variables in a VBscript during development process? (e.g. in a continuously opened command window)

I need to write a huge VBscript to automatically run an application and I'm looking for a way to comfortably monitor what I'm actually doing, in other words, to display the values of some/all variables involved in my script.
I'm used to work with Matlab, where I have a comfortable workspace browser. When I run a Matlab script, all variables, their types and their values are accessible in that workspace and can be checked.
The VBscript I write with Notepad++ (it needs to be a free editor) and the only way I found to display variables was echoing them via wscript and cscript.
I set up the shortcuts.xml with the following line to run my script directly from Notepad++:
<Command name="Run with CScript" Ctrl="yes" Alt="no" Shift="yes" Key="116">cmd /K %windir%\system32\cscript.exe "$(FULL_CURRENT_PATH)"</Command>
In case I include commands in my script like
Wscript.Echo myVar
Wscript.Echo "Hello World!"
and run it with the newly introduced shortcut, a cmd window pops up and displays the value of myVar and "Hello World!".
But the next time I run the script a new window pops up. So my question is:
Is it possible get a continuously opened output window, displaying all echoed values everytime I run a script? I actually want to put the window on a second screen and keep the values from previous runs. So I can enter a line Wscript.Echo something, run, check, enter something else and so on, without fiddling around with a bunch of opened windows.
Alternatively, is there any open-source/free editor which offers an accessible workspace like the one in Matlab?
The open-source editor SciTE offers what I was looking for.
The default settings in vb.properties enable a similar behavior like in Notepad++
command.build.$(file.patterns.wscript)=cscript "$(FilePath)"
command.build.subsystem.$(file.patterns.wscript)=1
One can change it as follows to get the output into the integrated console.
command.go.$(file.patterns.wscript)=cscript.exe //nologo "$(FilePath)"
command.go.subsystem.$(file.patterns.wscript)=0
F5 runs the script and Shift+F5 cleans the output.
Another option is the NppExec Plugin for Notepad++ suggested by #Ansgar Wiechers, which adds a console. The script can be run with cscript.exe /nologo "$(FULL_CURRENT_PATH)" then.
Use a debugger. Start your script with the (meta)option //X. If you are lucky, you already have installed software (MS Office, Visual Studio (Express)) that provides a debugger for VBScript. If not do a bit of research to find an Express version suitable to your OS.
You can almost write native VBScript in the VBA editor, so if you have Excel or whatever you can use this to debug, then go through some steps to convert back to VBScript. That's what I usually do.

Creating and running a Vbs Script VIA a windows form

I have a Vbs script to install network printers at my place of work. The only issue is that it needs to be hard coded with the printer and server name every time I want to use it. So as a solution I was trying to figure out if I could make a simple Windows form with a text box or drop down list that would simply push the input from there into the vb script and then run it. I can't figure out how to do this for the life of me though. Here is my VB script Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\wcprt02\S100a"
'objNetwork.SetDefaultPrinter "\wcprt02\L30b"
Your options are:
Use WScript.Arguments to pass (a few) parameters to your script started with cscript.exe from a console (DOS Box)
Read the arguments from a file (maintained via the editor of your choice; or Excel if you prefer)
Use InputBox to gather (a few) parameters via simple dialogs/pop-ups
Write a .HTA application presenting a GUI for (maybe many) arguments; pass them to your code embedded as a function/sub in the .hta
Write a .NET Forms application presenting such a GUI; you could then shell out to your script, but why not stay in .NET and avoid the mixing of technologies?

General Question about a GUI to execute a batch file

I'm new to programming and have taken some classes in it so I'm not sure if this is possible. I want to use a COTS software called 010 Hex Editor and write a script for the program to automate a couple tasks. These tasks can be run from a batch file according to their documentation.
Then I want to instruct the user to eject the device and reinsert it to clear the cache. (This I want done just with a simple pop-up window.
Then I also want to format the device which I assume can be done in a batch file as it can be done through dos on Windows.
My question is, with these 3 steps, can I build a simple GUI that has a button that says, "Start" or something like that, then the pop up window comes up to eject and reinsert, and then another button to format, and another button to exit. I'm not really familiar with what language this could be done in, and how to do this as the C++/Java classes I've taken have been more about syntax and OOP. Thanks!
You can use the windows messenger service to put up an alert - but on newer windows (vista/7) it's a pain to set all the permissions to allow this.
There are lots of free utilities that will popup a dialog from the command line, with a given message and wait for a response.
They are generally called messagebox or msgbox - sorry can't recommend any in particular

Resources