![]() |
AppActivate runtime error 5 when used with shell cmd
When I use AppActivate to set focus to a console window started with
Shell, I receive runtime error 5. This doesn't occur when I put a break point on the AppActivate statement and then resume. dblPID = Shell("cmd.exe /k", vbNormalFocus) AppActivate dblPID ' Runtime error 5 Is this a bug? I also tried putting in a for-next loop to insert a delay between the statements. And in the real program, I have several statements between the Shell and the AppActivate statements. I'm trying to get the VBA code to wait for the shell call to complete before certain statements get executed. Do I have to resort to using an API shell call? I'm running Win2000 SP4, Office 2000 SP3. Here's some simple code that demonstrates how I think this should work, but note that the real code uses the /c switch to cmd and includes several statements between the Shell and AppActivate calls: Private Sub cmdOK_Click() Dim dblMinVal As Double dblMinVal = Shell("cmd.exe /K", vbNormalFocus) On Error Resume Next Do AppActivate dblMinVal If Err.Number = 5 Then ' Console closed Err.Clear Exit Do End If Loop On Error GoTo 0 MsgBox "You shouldn't see this message until " & _ "you close the console window, but you do!" Unload frmMain End Sub 'cmdOK_Click() (Note that my real email address is rzeitler AT phonon DOT com.) |
AppActivate runtime error 5 when used with shell cmd
rayz wrote on 04/08/2004 11:16 ET :
When I use AppActivate to set focus to a console window started with Shell, I receive runtime error 5. This doesn't occur when I put a break point on the AppActivate statement and then resume. dblPID = Shell("cmd.exe /k", vbNormalFocus) AppActivate dblPID ' Runtime error 5 Is this a bug? I also tried putting in a for-next loop to insert a delay between the statements. And in the real program, I have several statements between the Shell and the AppActivate statements. I'm trying to get the VBA code to wait for the shell call to complete before certain statements get executed. Do I have to resort to using an API shell call? I'm running Win2000 SP4, Office 2000 SP3. Here's some simple code that demonstrates how I think this should work, but note that the real code uses the /c switch to cmd and includes several statements between the Shell and AppActivate calls: Private Sub cmdOK_Click() Dim dblMinVal As Double dblMinVal = Shell("cmd.exe /K", vbNormalFocus) On Error Resume Next Do AppActivate dblMinVal If Err.Number = 5 Then ' Console closed Err.Clear Exit Do End If Loop On Error GoTo 0 MsgBox "You shouldn't see this message until " & _ "you close the console window, but you do!" Unload frmMain End Sub 'cmdOK_Click() (Note that my real email address is rzeitler AT phonon DOT com.) the error happens because when AppActivate is executed, the application opened by "Shell" is not running yet. Insert "Sleep (2000)" between Shell and AppActivate like below X = Shell("cmd.exe", 1) Sleep (2000) AppActivate X, False |
All times are GMT +1. The time now is 02:49 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com