Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regarding Shell function

Posted on 1998-08-24
2
Medium Priority
?
279 Views
Last Modified: 2010-04-30
As explained in VB Help, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.

But I would like to finish the Shell function before execute the next statement.  Currently I am using Call statement to execute Shell function, will this overcome the above problem?
0
Comment
Question by:cwchaw
[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
2 Comments
 
LVL 1

Accepted Solution

by:
jsuper earned 100 total points
ID: 1430806
I'm not sure if the above method will solve your problem, but there are a number of ways to call a program and wait for it to complete.  I pulled the following code from the Visual Basic Developers Resource Centre.  (http://www.mvps.org/vbnet/)  There are some other methods at the site you can try as well, but this should work fine...

Add the following code to a code module...

Option Explicit

Public Const NORMAL_PRIORITY_CLASS = &H20&Public Const INFINITE = -1&

Public Type STARTUPINFO
   cb As Long

   lpReserved As String
   lpDesktop As String
   lpTitle As String
   dwX As Long
   dwY As Long
   dwXSize As Long
   dwYSize As Long
   dwXCountChars As Long
   dwYCountChars As Long
   dwFillAttribute As Long
   dwFlags As Long
   wShowWindow As Long
   cbReserved2 As Long
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type

Public Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessId As Long
   dwThreadID As Long
End Type

'Note: the number of line continuations in
'the next declare exceed VBs maximum; the API is  
'presented this way here only for clarity.
Public Declare Function CreateProcessA Lib "kernel32" _
   (ByVal lpAppName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long,
     ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

And add this code to whatever form or module you will be using to shell out to other applications.  In this case, replace the call to the notepad.exe with whatever program you are trying to call...  Also, the example assumes you are using a command key to trigger the calling of the external program.  Just move the
RunProcess command to whatever subroutine you desire...

Option Explicit

Private Sub Command1_Click()

    RunProcess "c:\windows\notepad.exe"

End Sub


Private Sub RunProcess (cmdline As String)

    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim r As Long

   
  'Initialize the STARTUPINFO structure by
  'passing to start the size of the STARTUPINFO
  'type. Setting the  .cb member is the only
  'item of the structure needed to launch the program
   start.cb = Len(start)
   
  'Start the application
   Call CreateProcessA(0&, cmdline, 0&, 0&, 1&, _
                       NORMAL_PRIORITY_CLASS, 0&, 0&, _
                       start, proc)
   
  'Wait for the application to finish
   Call WaitForSingleObject(proc.hProcess, INFINITE)
   
  'Close the handle to the process
   Call CloseHandle(proc.hProcess)

   MsgBox "The Shelled process " & cmdline & " has ended."

End Sub



Give that a shot, hopefully it will perform as you expected.   Good Luck.
0
 
LVL 1

Expert Comment

by:disha
ID: 1430807
Call will not solve the problem. The way is to poll for that application to finish after shell command. This can be done using getmoduleusage API call....and checking if u'r application (started with shell) is still running..
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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