Solved

Catch Cancellation error from objShell.Run

Posted on 2016-08-27
10
94 Views
Last Modified: 2016-09-22
I have a script which installs programs. Everything works fine. But if the user cancels the installation a script error occurs. Is there anyway we can end the script with a custom message.

Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
Set objShell = CreateObject("WScript.Shell")
objShell.Run strPath & "\McAfee\XP\MTPForXP.exe"            

Once the program is executed a security warning comes which say's do you want this program to make changes to your computer. if the user selects yes the script exits and the program installs and everything is fine.

But if the user Clicks No a script error occurs. Is there anyway we can end the script with a custom message.
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
10 Comments
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 41773020
.Run returns the result, if you set the third parameter bWaitOnReturn to true. A non-zero result means a warning or error.
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
Set objShell = CreateObject("WScript.Shell") 
result = objShell.Run(strPath & "\McAfee\XP\MTPForXP.exe",1,true)

Open in new window

0
 
LVL 84

Accepted Solution

by:
oBdA earned 375 total points
ID: 41773054
Try something like this:
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
On Error Resume Next
intErrorLevel = objShell.Run(strPath & "\McAfee\XP\MTPForXP.exe", 1, true)
If Err.Number <> 0 Then 
	If Err.Number = -2147023673 Then
		MsgBox "Elevation declined by user, installation canceled!"
	Else
		MsgBox "Error " & Err.Number & vbCRLF & "Source: " & Err.Source & vbCRLF & "Description: " & Err.Description
	End If
	Err.Clear
Else
	If intErrorLevel = 0 Then
		MsgBox "Installation was successful."
	ElseIf intErrorLevel = 3010 Then
		MsgBox "Installation was successful, but requires a reboot."
	Else
		MsgBox "Installation finished with errorlevel " & intErrorLevel
	End If
End If
On Error Goto 0

Open in new window

0
 

Author Comment

by:Venkata Ramesh Pagadala
ID: 41773072
Thanks a lot for the help. Works beautifully.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:oBdA
ID: 41794563
Sorry, but I'm afraid I have to object to the Close Request (not because I mind sharing points, but because the suggested accepted answer would not answer the question correctly).

Qlemo,
the initial question was [...] a security warning comes which say's do you want this program to make changes to your computer. [...] But if the user Clicks No a script error occurs. Is there anyway we can end the script with a custom message.

This is the UAC prompt - and a "No" to an UAC prompt will not be caught by a simple bWaitOnReturn, but result in a script error, without the possibility to display a custom message (try it, for example, with regedit.exe using an administrative user in a non-elevated shell). So implementing the "On Error Resume Next" is not "adding more flesh to [your] code", it's a requirement.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41794574
I didn't read the script error as being an UAC prompt, but likely it is.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41802970
If the OP will not provide anything, my adjusted suggestion is
   https:#a41773020   125
   https:#a41773054   375
as oBdA is probably correct regarding UAC prompts, but my code is still part of the solution.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41803271
No objection, your honor.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question