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:
'Purpose : Holds execution until application has finished opening
'Inputs : sCommandLine = The Command line to run the application e.g. "Notepad.exe"
' lState = The Window State to run of the shelled program (A Long)
'Outputs : Returns the Process Handle
'Notes : Use this when you want to wait for an application to finishing opening before proceeding
' The side effects mentioned in ShellAndHold will be negligible since the most applications
' load in under 5 seconds.
Function ShellAndWaitReady(sCommandLine As String, Optional lState As Long = vbNormalFocus) As Long
Dim lhProc As Long
If Left$(sCommandLine, 1) <> Chr(34) Then
sCommandLine = Chr(34) & sCommandLine
End If
If Right$(sCommandLine, 1) <> Chr(34) Then
sCommandLine = sCommandLine & Chr(34)
End If
lhProc = Shell(sCommandLine, lState)
'Wait for the process to initialize
Call WaitForInputIdle(lhProc, INFINITE)
'Return the handle
ShellAndWaitReady = lhProc
End Function
Private Sub cmdOutlook_Click()
Dim fileName As String
Dim retrunValue As Long
fileName = "C:\Program Files\Microsoft Office\OFFICE\outlook.EXE"
retrunValue = ShellAndWaitReady(fileName)
'FROM THIS POINT ON IS WHERE THE REST OF THE CODE IS EXECUTING
End Sub
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
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.
Re: ShellAndWaitReady Function - Please Help :)
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
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?
Re: ShellAndWaitReady Function - Please Help :)
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.
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.