SendKeys

I am trying to send information to another program that I do not control.  I can use AppActivate to send information to the application.  Question -- how do I send the information to a child form of the application?
VBStarterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
You would need to enumerate the child windows of the application in order to obtain the hWnd of the desired child, then send the keystrokes to that window. Depending on the app, there may be other ways. It might be possible to use DDE to send info rather than sendkeys for example.
0
VBStarterAuthor Commented:
Thanks for the comment, now a couple of question on your comment.  How do I enumerate the child windows when I did not write the MDI app?  How do I use sendkeys for sending information to a child window? Thanks.
0
KhollADCommented:
You can try the code below :

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CHILDACTIVATE = &H22
Private Const WM_SETFOCUS = &H7

Private Sub Command1_Click()

Dim lRet As Long

Dim P_hWnd As Long
Dim M_hWnd As Long
Dim C_hWnd As Long

Dim MDIAppTitle As String
Dim ChildCaption As String

'The MDI app title that you want
MDIAppTitle = "MDIApp Title"


'The Child title that you want
ChildCaption = "MDIChild1"


'Find then parent MDI
P_hWnd = FindWindow(vbNullString, MDIAppTitle)

'Find then Childs 'container' (don't change it)
M_hWnd = FindWindowEx(P_hWnd, 0&, "MDIClient", vbNullString)


'Find the Child that you want
C_hWnd = FindWindowEx(M_hWnd, 0&, vbNullString, ChildCaption)


'Activate the MDI App
AppActivate MDIAppTitle, True

'Set the focus to the child
lRet = SendMessage(C_hWnd, WM_SETFOCUS, 0&, 0&)

'show up the child (not needed, if you want, you can delete the line below)
lRet = SendMessage(C_hWnd, WM_CHILDACTIVATE, 0&, 0&)
   
'Send the key
SendKeys "{DELETE}", True


End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBStarterAuthor Commented:
Thanks for the code. I could not use the FindWindowEX because I do not know the child's caption. Is there a way to get the caption. I used GetWindowText after getting the child's hwnd, but I did not seem to work.
 
  I used the API GetTopWindow to get the child's hwnd and then I used the SendMessage to etfocus and activate the child. This seems to work. Does anyone see any problem with using the GetTopWindow function.
0
KhollADCommented:
Thanks.
its a good way, I don't see any problems with GetTopWindow.

You can use also the GetWindow Api to enum the childs :

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CHILDACTIVATE = &H22
Private Const WM_SETFOCUS = &H7


Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2



Private Sub Command1_Click()

    Dim Child() As Long
    Dim x As Long
    Dim i As Long
   
    Dim MIDAppTitle As String
   
    Dim p_hWnd As Long
    Dim m_hWnd As Long
    Dim c_hWnd As Long
   
    Dim lRet As Long
    Dim dt As Date 'or As Variant for old VB versions
   
   
    MDIAppTitle = "MDIApp Title"
   
    'Find then parent MDI
    p_hWnd = FindWindow(vbNullString, MDIAppTitle)
   
    'Find then Childs 'container' (don't change it)
    m_hWnd = FindWindowEx(p_hWnd, 0&, "MDIClient", vbNullString)


    'Activate the MDI App
    AppActivate MDIAppTitle, True

    'Get the First Child
    c_hWnd = GetWindow(m_hWnd, GW_CHILD)
   
    x = 0
    'Loop childs
    Do Until c_hWnd = 0
   
        'Stores child
        ReDim Preserve Child(x) As Long
        Child(x) = c_hWnd
       
        'Get the next child
        c_hWnd = GetWindow(c_hWnd, GW_HWNDNEXT)
        x = x + 1
    Loop

    For i = 0 To x - 1
        'Set the focus to the child
        lRet = SendMessage(Child(i), WM_SETFOCUS, 0&, 0&)
       
        'show up the child (not needed, if you want, you can delete the line below)
        lRet = SendMessage(Child(i), WM_CHILDACTIVATE, 0&, 0&)
           
        'Send the key
        SendKeys "{DELETE}", True
       
        'Wait for 4 seconds to continue (only for demonstration)
        dt = Now
        Do Until Now > DateAdd("s", 3, dt)
            DoEvents
        Loop
    Next
   
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.