Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WshShell.Run fails - How do I catch its error? 1

Status
Not open for further replies.

DPlank

IS-IT--Management
May 30, 2003
1,903
GB
Hi

I'm using the Run command to start a program in a VBScript function - but what do I do to gracefully capture a problem and exit the function in the event the program's not installed on the host machine?

It seems to generate a run-time exception saying the system's been unable to find the file, but the error code and source are (null) and the return value from the Run method doesn't indicate any error state so far as I can see?

My code is:
Code:
oWshell.run CMD, DISPLAY_WINDOW, DONT_WAIT_ON_RETURN
WScript.Sleep 3000
Set oWshell = Nothing

I'm specifically ensuring CMD points to a nonexistent program so I can try to stop the error occurring and handle it gracefully.

Any suggestions?

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
What about this ?
Code:
rc = oWshell.Run(CMD, DISPLAY_WINDOW, True)
If rc <> 0 Then
  MsgBox CMD & " failed with error " & rc
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi, thanks for the suggestion!

Unfortunately in the evaluation of the rc (the actual run command) the runtime exception occurs and the function halts, which I want to avoid. I tried to put the statement into a Try..Catch block but it didn't seem to work.

Any other ideas?

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
What is the exact error message ?
What is the value of CMD ?
Have you tried to play with the WshShell.Exec method ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The box that pops up on the screen contains the following:
Line: 0
Column: 0
Error: (null)
Code: 80070002
Source: (null)

System: The system cannot find the file specified.

CMD is currently set to point to a program that doesn't exist, because I need to handle this situation properly, although setting it to the correct program path works correctly. The concern needing resolved is that if the host PC the script runs on doesn't have the client app installed, I want to exit properly, and feed back the error message.

I'm looking at the Exec method just now to see if it will be any more use than Run. Thanks for the idea!

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
Running with the Exec method has the same runtime error popping up...

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
What about this ?
rc = oWshell.Run("cmd /c " & CMD, DISPLAY_WINDOW, True)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I've taken the easy way out - easy being the one you think of ages after posing the question...

I wrapped the Run command in an If block that verifies if the program file is present before attempting to run it. Stands to logical reason if the app file isn't in there, then the run command wouldn't work, and so I can sidestep the exception that way.

Thanks for the help PH - star for your efforts [smile]

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top