Hi, can you help,
I am trying to write code that can "Sendkeys" to another application for updates (not an ideal scenario, I know). I have mastered this successfully, but with fixed time delays between each update. This works well but can't cater for a slow network. The problem I have is that how do I know when the other application has finished processing each update.
Searching through this forum, I can find many examples of demonstrating the "WaitForSingleObject" API call. But they all seem to use a "Shell" startup reference. My other application will not be opened by me directly. All applications will be Windows based on a NT platform. Using my limited VB and API knowledge, I wrote this code ......
******************************************************
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long)As Long
Const WAIT_FAILED = &HFFFFFFFF
Const WAIT_OBJECT_0 = &H0
Const WAIT_TIMEOUT = &H102
Sub try_me()
Dim hWnd As Long 'receives handle to window.
Dim retval As String 'generic return value.
hWnd = FindWindow(vbNullString, "Microsoft Excel - Book1"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If hWnd = 0 Then 'window not found.
MsgBox ("Target Window Not Open"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Else
retval = WaitForSingleObject(hWnd, 2000)
Select Case retval
Case WAIT_OBJECT_0: 'The process terminated.
MsgBox ("process terminated"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Case WAIT_TIMEOUT: 'The process did not terminate.
MsgBox ("timeout"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Case WAIT_FAILED: 'Bad call to function.
MsgBox ("failed"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
End Select
End If
End Sub
******************************************************
Yes, I know its crude, but, I get a WAIT_FAILED return every time its run. I suspect its a misuse of the API calls.
Am I miles of the mark or barking up the wrong tree ? Any help is much appreciated.
Thanks, TopJack.
I am trying to write code that can "Sendkeys" to another application for updates (not an ideal scenario, I know). I have mastered this successfully, but with fixed time delays between each update. This works well but can't cater for a slow network. The problem I have is that how do I know when the other application has finished processing each update.
Searching through this forum, I can find many examples of demonstrating the "WaitForSingleObject" API call. But they all seem to use a "Shell" startup reference. My other application will not be opened by me directly. All applications will be Windows based on a NT platform. Using my limited VB and API knowledge, I wrote this code ......
******************************************************
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long)As Long
Const WAIT_FAILED = &HFFFFFFFF
Const WAIT_OBJECT_0 = &H0
Const WAIT_TIMEOUT = &H102
Sub try_me()
Dim hWnd As Long 'receives handle to window.
Dim retval As String 'generic return value.
hWnd = FindWindow(vbNullString, "Microsoft Excel - Book1"
If hWnd = 0 Then 'window not found.
MsgBox ("Target Window Not Open"
Else
retval = WaitForSingleObject(hWnd, 2000)
Select Case retval
Case WAIT_OBJECT_0: 'The process terminated.
MsgBox ("process terminated"
Case WAIT_TIMEOUT: 'The process did not terminate.
MsgBox ("timeout"
Case WAIT_FAILED: 'Bad call to function.
MsgBox ("failed"
End Select
End If
End Sub
******************************************************
Yes, I know its crude, but, I get a WAIT_FAILED return every time its run. I suspect its a misuse of the API calls.
Am I miles of the mark or barking up the wrong tree ? Any help is much appreciated.
Thanks, TopJack.