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