Consulting

Results 1 to 9 of 9

Thread: CMD.exe window keeps "taking over" when looping

  1. #1

    CMD.exe window keeps "taking over" when looping

    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.

  2. #2
    which loop ?

    what is Meshelleandwait stand for ?

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    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.

  5. #5
    Bump (hopefully that is allowed).

    Maybe there is just no way to address this?

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  7. #7
    - 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 :/

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Maybe try Activate or
    Windows(1).WindowState = xlMaximized
    if you want focus set elsewhere.

  9. #9
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •