Solved

Change ShellAndWait to not wait

Posted on 2014-07-19
2
172 Views
Last Modified: 2014-07-20
So I have the following function that works great:

Public Sub ShellAndWait(ByVal program_name As String, _
                         Optional ByVal window_style As VbAppWinStyle = vbNormalFocus, _
                         Optional ByVal max_wait_seconds As Long = 0)
    Dim lngProcessId As Long
    Dim lngProcessHandle As Long
    Dim datStartTime As Date
    Dim lngCursor As Long
    Const WAIT_TIMEOUT = &H102
    Const SYNCHRONIZE As Long = &H100000
    Const INFINITE As Long = &HFFFFFFFF

    lngCursor = Screen.MousePointer
    'Screen.'MousePointer = vbHourglass
    ' Start the program.
    On Error GoTo ShellError
    lngProcessId = Shell(program_name, window_style)
    On Error GoTo 0
    
    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    lngProcessHandle = OpenProcess(SYNCHRONIZE, 0, lngProcessId)
    If lngProcessHandle <> 0 Then
        datStartTime = Now
        Do
          If WaitForSingleObject(lngProcessHandle, 250) <> WAIT_TIMEOUT Then
            Exit Do
          End If
          DoEvents
          If max_wait_seconds > 0 Then
            If DateDiff("s", datStartTime, Now) > max_wait_seconds Then Exit Do
          End If
        Loop
        CloseHandle lngProcessHandle
    End If
    Screen.MousePointer = lngCursor
    
    Exit Sub
    
ShellError:
End Sub

Open in new window


I would like to change it so that it does the same thing but doesn't wait for the process to finish.  I still want it to finish, but in the background.  

Any ideas?
0
Comment
Question by:hrolsons
2 Comments
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 250 total points
ID: 40207156
At this link, http://www.cpearson.com/excel/ShellAndWait.aspx
it mentions you can use Shell function to do command and don't wait for the command complete. Is Shell function what your want. The ShellAndWait function is target to improve Shell function to wait the command finish before go to next step

Actually, you can use Shell function that can be programmed to wait or not wait for comand complete , you can find it at Wait option from MS manual at  http://msdn.microsoft.com/en-us/library/xe736fyk(v=vs.90).aspx

Hope understand your question. If not , please point it out

Duncan
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40207215
Simply call Shell alone instead of calling ShellAndWait:

Shell(program_name, window_style)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

685 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