VB icon

Best Shell & Wait (No API's!)

Email
Submitted on: 1/6/2015 7:09:00 PM
By: Matthew Roberts (from psc cd)  
Level: Intermediate
User Rating: By 16 Users
Compatibility: VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 2322
 
     Makes it easy to perform a clean "Shell & Wait" where your applicatoin kicks off an external application and waits for it to return before continuing. Many shell & wait examples I have found tend to overdrive the proccessor in a loop or require you to make API calls. This one uses the Windows Script object to take advantage of it's built-in wait parameter on the .Run method...scripting's version of Shell.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Best Shell & Wait (No API's!)
' Description:Makes it easy to perform a clean "Shell & Wait" where your applicatoin kicks off an external application and waits for it to return before continuing. Many shell & wait examples I have found tend to overdrive the proccessor in a loop or require you to make API calls. This one uses the Windows Script object to take advantage of it's built-in wait parameter on the .Run method...scripting's version of Shell.
' By: Matthew Roberts (from psc cd)
'
' Inputs:FileName - The name of the file you wish to run with any required switches included.
'
' Returns:True if the file was run and returned.
False if there was a file open or save error.
EXAMPLE: ShellAndWait ("notepad.exe c:\temp\teset.txt)
'
' Assumes:Should work on any Windows 98 machine. Others may need to get the newest VB service pack or install Windows Scripting Host (http://msdn.microsoft.com/scripting/jscript/download/55beta.htm). This is also included in Internet Explorer 5. If you already have IE5, this will work and it will be included when you build your setup file for distribution.
'
' Side Effects:None - Will not block other applications or overdrive the proccessor.
'**************************************

Function ShellAndWait(FileName As String)
Dim objScript
On Error GoTo ERR_OpenForEdit
Set objScript = CreateObject("WScript.Shell")
' Open a file for editing in Notepad and wait for return.
'The second parameter (after the FileName) is the Display Mode (normal w/focus,
'minimized...even hidden. For more info visit:
'http://msdn.microsoft.com/scripting/windowshost/doc/wsMthRun.htm
' The third parameter is the "Wait for return" parameter. This should be set to
' True for the Wait.
ShellApp = objScript.Run(FileName, 1, True)
ShellAndWait = True
EXIT_OpenForEdit:
 Exit Function
ERR_OpenForEdit:
 MsgBox Err.Description
 GoTo EXIT_OpenForEdit
End Function


Other 25 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.