VBScript process launch results in empty labels - vbscript

I am attempting to create a program for a business owner who is not good with computers. Trying to keep it simple, I just created a simple script to launch the program and send the password and tab/enter keys to login for him.
After a lot of trial and error I managed to get it to launch, but it does not come with any text where it should be, and the OK button does not launch the application.
Here is the code I use to launch the process:
Function AddQuotes(input)
AddQuotes = Chr(34) & input & Chr(34)
End Function
Set oShell = WScript.CreateObject("WScript.Shell")
progDir = oShell.ExpandEnvironmentStrings("%PROGRAMFILES%")
pPath = progDir & "\PELCO\PELCO DX4500_DX4600 Client\Network Client\DX4500_DX4600Client.exe"
msgbox AddQuotes(pPath), 1, "test"
oShell.Run AddQuotes(pPath), 1, false
The program will launch, but looks like this:
This is what the window should look like (as when launched directly from a shortcut):

Related

Open Microsoft Word and type text with SendKeys

I am trying to create a VBScript that opens Word and inputs text into it.
My code:
::Set wshshell = WScript.CreateObject("WScript.Shell")
::Set objShell = CreateObject("WScript.Shell")
::objShell.AppActivate "word"
::userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")
::WScript.Sleep 10000
::wshshell.SendKeys "Hello World!"
Perhaps surprisingly, you cannot interact with Word unless you actually started the program in the first place. As documented the AppActivate method activates an application window, i.e. brings the window of an already running application to the foreground.
To start Word programmatically use something like this:
Set wd = CreateObject("Word.Application")
wd.Visible = True
You can open a document like this:
Set doc = wd.Documents.Open("C:\path\to\your.docx")
or create a new document like this:
Set doc = wd.Documents.Add
Text can be entered for instance via the TypeText method:
doc.Selection.TypeText "some text"
Save the document like this:
doc.Save
doc.Close
or like this (if it's a new document or you want to save it under a different name/path):
doc.SaveAs "C:\path\to\new.docx", 16
doc.Close
Exit from the application like this:
wd.Quit
Do NOT use SendKeys for anything. EVER. Not unless you're being forced at gunpoint.
Also do NOT write Frankenscript that mixes different languages (like batch and VBScript) in the same file. It's a pain in the rear to debug and maintain.

Activating (bring to foreground) a specific window with vbscript

I'm not even sure where to start with my question, I tried a hundred things and googled for hours but didn't find anything useful. (I'm open to every dirty trick.)
Here's my problem:
I have a .hta-file with a listbox that looks like this:
It lists all sessions/modi of my SAP Gui running.
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
If application.Connections.Count > 0 Then
Set connection = application.Children(0)
If connection.Sessions.Count > 0 Then
Set session = connection.Children(0)
End If
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Set optGroup = Document.createElement("OPTGROUP")
optGroup.label = "Server"
'count all connected servers
ConnectionCount = application.Connections.Count
If ConnectionCount > 0 Then
Sessionlist.appendChild(optGroup)
Else
optGroup.label = "No connection here."
End If
'count all sessions per server
If ConnectionCount > 0 Then
For Each conn in application.Connections
'Text output connections and sessions
SessionCount = conn.Sessions.Count
whatIsIt = conn.Description
ConnectionFeld.innerhtml = ConnectionFeld.innerhtml & " <br> " & SessionCount & " Sessions auf " & whatIsIt
'fill listbox with all connections
Set objOption = nothing
Set optGroup = Document.createElement("OPTGROUP")
optGroup.label = conn.Description
Sessionlist.appendChild(optGroup)
i = 0
'fill listbox with all sessions
For Each sess In conn.Sessions
i = i + 1
Set objOption = Document.createElement("OPTION")
objOption.Text = "Session " & i & ": " & sess.ID
objOption.Value = sess.ID
SessionList.options.add(objOption)
Next
Next
Else
Exit Sub
End If
My goal: When I doubleclick on one of the entries in that list, the selected instance of my SAP Gui should come to the foreground/get activated.
Unfortunately my taskmanager only lists one task and that is "SAP Logon". One of my opened windows also has the name "SAP Logon", all others have the same name: "SAP Easy Access".
The only way I can see the IDs of the connection (servername) and the IDs of the session is via extracting them with vbscript. (see above)
Is there any way to do that? The only workarounds I could think of after trying a thousand solutions are these two:
extremely ugly workaround:
If sessionID = sess.ID Then
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
sess.findById("wnd[0]").maximize
End If
It minimizes all windows an then maximizes the selected SAP window. Unfortunately My HTA-GUI also gets minimized which kinda sucks.
Second idea:
Somehow get to these clickable thingies by shortcut and put that in my script or some other ugly way.
By hand you have to do this:
Click on that little arrow, rightclick on the icon and then leftclick on the name.
Is there any way to automate this? It's driving me crazy.
Hope someone can help me, it would be GREATLY appreciated.
PS: I'm sitting on a machine with restricted rights and so I may not be able to tackle this with Windows API-ish solutions.
EDIT concerning comments:
It is not possible:
to change registry entries
create COM objects
work with anything else than VBScript
Similarly, it also works with the following commands:
session.findById("wnd[0]").iconify
session.findById("wnd[0]").maximize
I found it...
The resizeWorkingPane method - for changing the size of a window - also works on windows in the background. If you change the parameters, the window will come to the foreground.
session.findById("wnd[0]").resizeWorkingPane 300,200,false
I have to partially revoke this, because it doesnt work on all windows. I'm still not sure why, but it keeps failing sometimes. Still, it seems to me, that this is the closest you can get.
From Help.
Activates an application window.
object.AppActivate title
object
WshShell object.
title
Specifies which application to activate. This can be a string containing the title of the application (as it appears in the title bar) or the application's Process ID.
I don't know what access to info you have about the window. Some COM objects have a HWnd property. This post gets you how to convert a hwnd to a ProcessID to be used above.
How to find the window Title of Active(foreground) window using Window Script Host
This shows how to convert a process command-line to a ProcessID. To see what properties and methods are available use the command-line tool wmic (wmic process get /? and wmic process call /?)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
msgbox objItem.ProcessID & " " & objItem.CommandLine
Next
This is a 100% of the time solution. It's ugly but it works. You can swap out the IQS3 t code for any other one you can confirm the user won't be in and will have access to. Also part of my reasoning for selection of this code is it loads fast.
Set objShell = CreateObject("wscript.shell")
session.findById("wnd[0]/tbar[0]/okcd").text = "/nIQS3"
session.findById("wnd[0]").sendVKey 0
objShell.AppActivate(cstr(session.ActiveWindow.Text))
session.findById("wnd[0]/tbar[0]/btn[3]").press

how to check that MS Word is running using VBScript

we have an application that runs MS Word (hidden) to print documents.
If one of the printers has a problem, then Word hangs while waiting for the spooler to return the 'queued' message.
We have found that if we make Word visible (by using VBA in Excel with GetObject and oWordApp.visible=true for example) then the process continues printing the other documents with no problem.
We would like to make this more automatic by having a VBScript check for Word in running processes, if it finds it, make it visible, wait for a few seconds, hide it, and quit...
But I have a problem that the VBScript GetObject function instantiates Word if it's not already running.
how should I check that word is running using VBScript without creating an instance of it?
here is the code I have in my VBScript file:
dim oWord, WScriptShell
set oWord = getobject("", "Word.Application")
set WScriptShell = CreateObject("WScript.Shell")
if isobject(oWord) then 'and oWord.Documents.count>0
wscript.echo("Word is running")
oWord.visible=true
WScript.Sleep 1000
oWord.visible=false
else
wscript.echo("Word not running")
end if
so what should I use to check if word is running without creating an instance of it?
Just change GetObject("", "Word.Application") to GetObject(, "Word.Application") and it doesn't start an instance.
But this throws an error if Word is not running, so you can use it like this:
msgbox wordIsRunning
function wordIsRunning
dim wdApp
on error resume next
set wdApp = GetObject(, "Word.Application")
wordIsRunning = (err.Number = 0)
set wdApp = nothing
end function
Word hangs
check "DisplayAlerts" property. allowed only if property "Visible" set to "True".
tasklist /FI "IMAGENAME eq winword.exe"
will show you, does m$word running or not

How to close an application when vbscript crashes

I'm using VBscript to open Microsoft Excel and convert xls documents to csv.
Here is a quick example that takes an argument and converts the first page
Dim oExcel
Dim oBook
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs "out.csv", 6
oBook.Close False
oExcel.Quit
If everything works, that's great. But when the script crashes before it can close excel, the process continues to stay around and lock the file until I manually kill the process.
How can I make sure that I perform any clean up routines even when the script fails?
As the question is/was? about closing Excel reliably when the VBScript used to automate it crashes:
If you write
Set oExcel = CreateObject("Excel.Application")
you create a 'simple' variable. VBScript may decrement a ref counter when the variable goes out of scope, but it certainly won't .Quit Excel for you.
If you want a feature like atexit calls or exception handling in VBScript, you'll have to write a class that 'does what I mean' in its Class_Terminate Sub. A simple example:
Option Explicit
Class cExcelWrapper
Private m_oExcel
Public Sub Class_Initialize()
Set m_oExcel = CreateObject("Excel.Application")
End Sub
Public Sub Class_Terminate()
m_oExcel.Quit
End Sub
Public Default Property Get Obj()
Set Obj = m_oExcel
End Property
End Class
Dim oExcel : Set oExcel = New cExcelWrapper
Dim oWBook : Set oWBook = oExcel.Obj.WorkBooks.Add()
oExcel.Obj.Visible = True
oExcel.Obj.DisplayAlerts = False
oWBook.Sheets(1).Cells(1,1) = "Div by Zero"
WScript.Echo "Check TaskManager & Enter!"
WScript.StdIn.ReadLine
WScript.Echo 1 / 0
(meant to be started with "cscript 20381749.vbs")
If you run this script with an open Taskmanager, you'll see Excel popup in the processes list (and on the screen, because of .Visible). If you then hit Enter, the script will abort with an "Division by Zero" error and the Excel process will vanish from the Processes list.
If you remove the .DisplayAlerts setting, Excel will ask you whether to save your work or not - proving thereby that the .Quit from the Class_Terminate() Sub really kicks Excel into byebye mode.
The class needs further work (basic settings, common actions (save?) before .Quit, perhaps a guard against misuse (Set oExcel = Nothing or other cargo cult crap), th .Obj addition isn't nice, and it won't help you if you kill your .vbs in a debugger, but for standard scenarios you won't see Excel zombies anymore.
Add "On Error Goto ErrorHandler" at the top of your script, and at the bottom of it, add "ErrorHandler:". Underneath the ErrorHandler label add code to manage the situation depending on the Err.Number
See Err object on MSDN.
You can also use "On Error Resume Next". Here is an example.
EDIT: My bad. "Goto" does not exist in VBS. The answer below is probably a much tidier approach.

How do I close Word (or other app) if an error occurs in a VBScript?

I wrote a VBScript app to open Word and Excel documents and search and replace blocks of text and various sections, pulling the new text from a plain text file. I purposely avoided any error checking, primarily because I couldn't figure it out at the time (and the script ran reliably anyway). Now months later on my local machine, I am inexplicably getting error messages about Normal.dot being changed and a message box asking what I want to do about it (which requires three more dialogs to finally answer). Of course this kills my ability to run the script and simply walk away, as it causes the script to fail. Currently when this happens, I have to open the Task Manager, find Winword.exe (of which the GUI isn't running) and kill it then re-run my script.
What's a reasonable way of catching the error and successfully shutting down Word (or Excel). Based on this question I'm trying this:
Set objDoc = objWord.Documents.Open(curDir1 + "\docs\template_spec.dot")
If Err.Number <> 0 Then
WScript.Echo "Error in Word Open:" & Err.Description
objWord.Quit
Else
Set objSelection = objWord.Selection
'Do replacement activities'
ReplaceText(objSelection)
objDoc.SaveAs(curDir1 + "\docs\mynewdocument.doc")
objWord.Quit
End If
Set objShell = Nothing
Set objWord = Nothing
Set objExcel = Nothing
Of course, as fate would have it, I cannot replicate the problem, so it works like normal. Does this solution seem reasonable? And a side question: How the heck do I get Word to stop complaining about Normal.dot (or get the script to handle it)? It's as if Word leaves itself open in the background after I have closed the GUI in some cases.
have you considered wrapping everything into an 'On Error Resume Next' statement so that your script ignores all the errors and continues to run as much as possible before calling the objWord.quit regardless of success or fail.
if you want more information on the correct use of 'On Error Resume Next' then go over to the msdn article on it!
Hope this helps!
Paul
I'm afraid that
WScript.Echo "..."
if it ever fires, is going to stall your script. Other than that, everything looks right. I'll play with it when I get home.
Edit: Word does hang out in the background, quite frequently. For one thing, if you use Outlook, and use Word as your Outlook editor, Word won't go away until Outlook is gone.
I'd agree with the use of "on error resume next".
If you really need to forcefully terminate Word, you can use WMI and the Win32_Process class to find and kill the process. This should be a last resort if everything else fails.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'winword.exe'")
For Each objProcess in colProcess
objProcess.Terminate()
Next
This was a modified example from:
http://www.computerperformance.co.uk/vbscript/wmi_process_stop.htm
Also, make sure all your references to the Word automation object are closed and/or set to nothing before you terminate the process.
The most reliable way to terminate all ActiveX instances, clean up garbage, and release resources is to put the code for that purpose into Sub Class_Terminate() of a dummy class, created instance of the class allows to handle script quit event.
Option Explicit
Dim objBeforeQuitHandler, objWord
' create a dummy class instance
Set objBeforeQuitHandler = New clsBeforeQuitHandler
' create word app instance
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Add.ActiveWindow.Selection.TypeText "80040000 error was raised. About to terminate the script." & vbCrLf & "Word will be quitted without saving before script termination just you close popped up error message."
' your code here...
' raise an error
Err.Raise vbObjectError
Class clsBeforeQuitHandler
' dummy class for wrapping script quit event handler
Private Sub Class_Terminate()
Dim objDoc
On Error Resume Next ' to prevent errors in case of unexpected word app termination
If TypeName(objWord) <> "Object" Then ' word app has not been closed yet
objWord.DisplayAlerts = False
For Each objDoc In objWord.Documents
objDoc.Saved = True ' to prevent save as dialog popping up
objDoc.Close
Next
objWord.Quit
End If
End Sub
End Class

Resources