Results 1 to 9 of 9

Thread: ShellAndWaitReady Function - Please Help :)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    ShellAndWaitReady Function - Please Help :)

    Hey everyone, hope your having a great day!

    I'm having some difficulty with a function I found called "ShellAndWaitReady" (http://www.visualbasic.happycodings....er/code38.html) but I can't seem to make it do what I want it to. Essentially what I need to happen is have MS Outlook be FULLY loaded before the rest of my code executes. Unfortunately, right now my code is continuing before the program loads and its messing things up. Does anyone have any suggestions? Thanks so much!


    VB Code:
    1. 'Purpose   :    Holds execution until application has finished opening
    2. 'Inputs    :    sCommandLine     =   The Command line to run the application e.g. "Notepad.exe"
    3. '               lState           =   The Window State to run of the shelled program (A Long)
    4. 'Outputs   :    Returns the Process Handle
    5. 'Notes     :    Use this when you want to wait for an application to finishing opening before proceeding
    6. '               The side effects mentioned in ShellAndHold will be negligible since the most applications
    7. '               load in under 5 seconds.
    8.  
    9.  
    10. Function ShellAndWaitReady(sCommandLine As String, Optional lState As Long = vbNormalFocus) As Long
    11.     Dim lhProc As Long
    12.    
    13.     If Left$(sCommandLine, 1) <> Chr(34) Then
    14.         sCommandLine = Chr(34) & sCommandLine
    15.     End If
    16.     If Right$(sCommandLine, 1) <> Chr(34) Then
    17.         sCommandLine = sCommandLine & Chr(34)
    18.     End If
    19.     lhProc = Shell(sCommandLine, lState)
    20.     'Wait for the process to initialize
    21.     Call WaitForInputIdle(lhProc, INFINITE)
    22.     'Return the handle
    23.     ShellAndWaitReady = lhProc
    24. End Function
    25.  
    26. Private Sub cmdOutlook_Click()
    27. Dim fileName As String
    28. Dim retrunValue As Long
    29.      
    30.     fileName = "C:\Program Files\Microsoft Office\OFFICE\outlook.EXE"
    31.     retrunValue = ShellAndWaitReady(fileName)
    32.  
    33. 'FROM THIS POINT ON IS WHERE THE REST OF THE CODE IS EXECUTING
    34. End Sub

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: ShellAndWaitReady Function - Please Help :)

    hi Give this a try

    Code:
    Option Explicit
    
    Private Const STATUS_PENDING As Long = &H103
    Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
    Private Const STILL_ACTIVE As Long = STATUS_PENDING
    
    Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    
    Private Declare Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Long, _
    ByRef lpExitCode As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    
    Public Function SHWait(ByVal ProgID As Long) As Boolean
    Dim mExitID As Long
    Dim hdlProg As Long
        
        hdlProg = OpenProcess(PROCESS_ALL_ACCESS, False, ProgID)
        GetExitCodeProcess hdlProg, mExitID
        
        Do While mExitID = STILL_ACTIVE
            DoEvents
            GetExitCodeProcess hdlProg, mExitID
        Loop
        
        CloseHandle hdlProg
        SHWait = mExitID
    End Function
    
    Private Sub Command1_Click()
    Dim iWait As Long, Done As Long
        
        iWait = Shell("C:\windows\system32\notepad.exe", vbNormalFocus)
        If (iWait) Then Done = SHWait(iWait)
        
        If (Done = -1) Then
            'Code here
        End If
        
    End Sub
    When your dreams come true.
    On error resume pulling hair out.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: ShellAndWaitReady Function - Please Help :)

    Thanks dreamvb for the reply! Correct me if I'm wrong, but won't this only execute the rest of the code once the application has been closed? If so, I need my code to execute before closing Outlook.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: ShellAndWaitReady Function - Please Help :)

    Anyone have any ideas?

  5. #5
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ShellAndWaitReady Function - Please Help :)

    Dont know if this will do what you want but maybe check it out ..
    http://www.vbforums.com/showpost.php...4&postcount=17

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: ShellAndWaitReady Function - Please Help :)

    Thanks rory for the post. However, when I try to run the program I get a compile error saying "Can't find project or library" and "Trim$" is selected. What am I missing? Is that a vb.net function?

  7. #7
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ShellAndWaitReady Function - Please Help :)

    this is VB6.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: ShellAndWaitReady Function - Please Help :)

    I figured out why it wasn't running, you forgot to add the VB6 PopupMenu DLL Binary in the zip file or the link to the download site (http://www.vbaccelerator.com/home/vb...DLL_Binary.asp) Anyway, now that I fixed it I'm going to test it out.

  9. #9
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ShellAndWaitReady Function - Please Help :)

    sorry my bad, ill take that out as its not used, i just copied most of it from an existing shell i used which utilized that pop up ..

    Uploaded modified without that reference.
    Last edited by rory; Aug 16th, 2006 at 08:51 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width