[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

SendKeys

Posted on 2000-01-25
5
Medium Priority
?
411 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:VBStarter
  • 2
  • 2
5 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2385455
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
 

Author Comment

by:VBStarter
ID: 2385629
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
 
LVL 3

Accepted Solution

by:
KhollAD earned 300 total points
ID: 2387594
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
 

Author Comment

by:VBStarter
ID: 2389277
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
 
LVL 3

Expert Comment

by:KhollAD
ID: 2389676
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question