I need to know how I can have my VB application wait until something is open after a shell line before the SendKeys line activates.
thanks.
Printable View
I need to know how I can have my VB application wait until something is open after a shell line before the SendKeys line activates.
thanks.
Different applications load differently. There is no 'finished loading' flag, meaning the best way to do what you want is to simply wait a specified amount of time before the SendKeys statement.
You can use the WaitForInputIdle function to get information when a process is ready to get input from the mouse or keyboard. However this function requires a hProcess handle which you don't get from the VB Shell function. You must eighter use CreateProcess or the ShellExecuteEx function.
I've written a wrapper sub that uses ShellExecuteEx to launch a file and then waits for the process to be ready to get input from the keyboard before it returns.Just call ShellAndWaitForReady and pass it the file to launch (this could be either a program or a document that has an associated program).VB Code:
Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFO) As Long Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long ' fields lpIDList As Long lpClass As Long hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Private Const SEE_MASK_NOCLOSEPROCESS = &H40 Private Const SEE_MASK_FLAG_NO_UI = &H400 Public Sub ShellAndWaitForReady(ByVal sFile As String) Dim hProcess As Long Dim shi As SHELLEXECUTEINFO Dim nRet As Long shi.lpDirectory = vbNullString shi.lpVerb = "Open" shi.lpParameters = vbNullString shi.lpFile = sFile shi.nShow = vbNormalFocus shi.fMask = SEE_MASK_FLAG_NO_UI Or SEE_MASK_NOCLOSEPROCESS shi.cbSize = Len(shi) If ShellExecuteEx(shi) > 32 Then hProcess = shi.hProcess Do nRet = WaitForInputIdle(hProcess, 500) DoEvents Loop While nRet <> 0 CloseHandle hProcess End If End Sub
Best regards
That's a really cool API, although it is very application specific. Better than my solution, though :lol:
Application specific? Yes, it only works on Windows application :) and of course all application might not react to keyboard input even though it's ready to do so... but to that kind of application you can't use SendKeys anyway.Quote:
Originally posted by jemidiah
That's a really cool API, although it is very application specific. Better than my solution, though :lol:
I tried it on Acrobat Reader 6, and it returned during the splash screen, which is why I said app. specific (mostly I was trying to insinuate they should, of course, test it on each app. they intended to use it on) ;)