quote:
Originally posted by Matty
code:
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) _
As Long
Public Const WM_CHAR = &H102
Public Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const INFINITE = &HFFFF
Public lngPrevWnd As Long
Public ConversationWnd_Obj As Object
Public Sub SendText(hwnd As Long, _
sText As String, _
Optional bSend As Boolean = False, _
Optional bKeepFocus As Boolean = True)
Dim RetVal As Long
RetVal = SetForegroundWindow(hwnd)
WaitForSingleObject RetVal, INFINITE
For i = 1 To Len(sText)
Call PostMessage(hwnd, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
Next i
If bSend Then
Call PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0&)
Call PostMessage(hwnd, WM_KEYUP, VK_RETURN, 0&)
End If
If Not bKeepFocus Then
Call SetForegroundWindow(lngPrevWnd)
End If
End Sub
IIRC, It isnt neccessary to set the windows focus to use post message to send keys.