PDA

View Full Version : CMD.exe window keeps "taking over" when looping



ronjon65
01-22-2016, 05:32 PM
I am using the following code within a loop:


retval = MeShellAndWait("cmd /c" & "\program.exe", vbNormalFocus)

---

The problem is that this loop can take a long time to complete. If I switch to another window, when it gets to the part of the code where the line above is run, then a cmd window becomes the active window. In other words, I can't run the loop "in the background". The desired approach is to run the loop without Excel or the command window becoming the active window. I tried the vbHide option, but that just hides the windows. It will still make Excel the active window when it gets to this line.

snb
01-23-2016, 05:43 AM
which loop ?

what is Meshelleandwait stand for ?

p45cal
01-23-2016, 09:09 AM
A stab in the dark; using one of the last 3 instead of the second.. from help:

vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.

ronjon65
01-23-2016, 11:24 AM
I didn't show the loop...lots of extra code. But I think you can just assume the cmd (shell) is being run inside the loop.

MeShellAndWait is the function runs the shell. Maybe there can be an adjustment there?


Function MeShellAndWait(szCommandLine As String, Optional iWindowState As Integer = vbHide) As Boolean

Dim lResult As Long
Dim lTaskID As Long
Dim lProcess As Long
Dim lExitCode As Long


On Error GoTo ErrorHandler

lTaskID = Shell(szCommandLine, iWindowState)

If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."

lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)

If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."

Do
'lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
lResult = GetExitCodeProcess(lProcess, lExitCode)
DoEvents
Loop While lExitCode = STILL_ACTIVE

MeShellAndWait = True

Exit Function

ErrorHandler:
gszErrMsg = Err.Number
MeShellAndWait = False
End Function



p45cal: I went ahead and tried all the options. Was worth a shot, but no luck. Every time the CMD window gets called, Excel becomes the active window.

ronjon65
01-28-2016, 01:07 PM
Bump (hopefully that is allowed).

Maybe there is just no way to address this?

Kenneth Hobs
01-28-2016, 01:17 PM
Are you running it from VBE or otherwise? Don't run it from VBE.

DoEvents may help.

I guess you can try Chip's routine: www.cpearson.com/Excel/ShellAndWait.aspx
or
http://www.mvps.org/access/api/api0004.htm
though they use similar methods.

ronjon65
01-28-2016, 02:08 PM
- Running it from a button (so not in VBE)

- I tried those two routines, but they behave the same (regardless of how I set the vbAppWinstyle). Darn :/

Kenneth Hobs
01-28-2016, 02:59 PM
Maybe try Activate or
Windows(1).WindowState = xlMaximizedif you want focus set elsewhere.

ronjon65
01-28-2016, 03:18 PM
Ken, wouldn't that activate a specific window? The goal is the let Excel loop run in the background (for hours) while still be being resume normal activities (which means using various windows).