AppActivate runtime error 5 when used with shell cmd

  • Thread starter Raymond Zeitler
  • Start date
R

Raymond Zeitler

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.)
 
R

Raymond Zeitler

I love newsgroups. I usually figure things out after posting in them.

After switching to two nested for-next loops, I was able to get
something to work. it's not elegant, but at least I have one less
thing on my plate.

Here's the new test code. Note the additional nested for-next loop
and the conditional that allows AppActivate to run only one time in
32000 loops:

Private Sub cmdOK_Click()
Dim dblMinVal As Double, i As Integer, j As Integer

dblMinVal = Shell("cmd.exe /C dir *.*", vbNormalFocus)

For i = 0 To 16000
For j = 0 To 16000
Next
Next

i = 0

On Error Resume Next
Do

i = i + 1
If i >= 32000 Then
i = DoEvents
AppActivate dblMinVal
If Err.Number = 5 Then ' Console closed
Err.Clear
Exit Do
End If
End If

Loop

On Error GoTo 0

MsgBox "You shouldn't see this message until " & _
"after the console window closes!"

Unload frmMain

End Sub 'cmdOK_Click()
 
L

login

rayz wrote on 04/08/2004 11:16 ET
When I use AppActivate to set focus to a console window started wit
Shell, I receive runtime error 5. This doesn't occur when I put
break point on the AppActivate statement and then resume

dblPID = Shell("cmd.exe /k", vbNormalFocus
AppActivate dblPID ' Runtime error

Is this a bug? I also tried putting in a for-next loop to insert
delay between the statements. And in the real program, I have severa
statements between the Shell and the AppActivate statements

I'm trying to get the VBA code to wait for the shell call to complet
before certain statements get executed. Do I have to resort to usin
an API shell call

I'm running Win2000 SP4, Office 2000 SP3. Here's some simple cod
that demonstrates how I think this should work, but note that the rea
code uses the /c switch to cmd and includes several statements betwee
the Shell and AppActivate calls


Private Sub cmdOK_Click(
Dim dblMinVal As Doubl

dblMinVal = Shell("cmd.exe /K", vbNormalFocus

On Error Resume Nex
D
AppActivate dblMinVa
If Err.Number = 5 Then ' Console close
Err.Clea
Exit D
End I

Loo

On Error GoTo

MsgBox "You shouldn't see this message until " &
"you close the console window, but you do!&quot

Unload frmMai

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 opene
by "Shell" is not running yet. Insert "Sleep (2000)&quot
betwee
Shell and AppActivate like belo

X = Shell("cmd.exe", 1
Sleep (2000
AppActivate X, False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top