Sending keys.

How would I send keys to another window that I know the name of?
i.e. - - -
In programA there is a command buttton that when you click it, it will open another program(programB). On that window there is a command button.  How would I make programA be able to push the button on programB using sendkeys?
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.

To set focus to ProgramA, use:

   AppActivate "title"

Once you have done that, do:

   SendKeys "{TAB}",TRUE

until the command button if focused.  For example, when you start ProgramA, if you have to hit the tab 4 times before the command button is selected, then you need to do the sendkeys 4 times...

Once you have focus on the command button, do this:

   SendKeys " ",TRUE

This will "press" the button...

master01Author Commented:
But how would I do it if the windows title varies between 2 names "Send Online Message" and "Send Offline Message" ?
master01Author Commented:
How would I do it though if the title of the window can be either "Send Online Message" or "Send Offline Message"?
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Do a AppActivate to both.

AppActivate "Send Online Message"
AppActivate "Send Offline Message"

No matter which window is open, it will switch to it.
master01Author Commented:
Yes, but then I get an error because there is no AppActivate with the other name.
Catch the error (5) and resume next...
Not too clean, but functional.
Another way would be to use the WINAPI calls.
With Findwindow
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

You can search for your window. If this window does not exist, the returnvalue will be 0 otherwise, the handle to your window is returned. You would not need that, you can assess the existence of your window with FindWindows and use AppActivate when is does.

Example :
in a form with a label1, text1 and a command1 put in the following code :
Option Explicit

Private sAppToFind As String

Private Sub Command1_Click()
    Dim l_hWnd As Long
    'sAppToFind = String(Len(sAppToFind), 0)
    sAppToFind = Text1.Text
    'sAppToFind = Trim(sAppToFind)
    l_hWnd = FindWindow(vbNullString, sAppToFind)
    If l_hWnd <> 0 Then
        AppActivate sAppToFind
        MsgBox "No such window"
    End If
End Sub
' ********END FORM*************
In a module add the code:

Option Explicit

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

' *******END MODULE *************


In your question, you SPECIFICALLY asked:

  "How would I send keys to another window that I know the name of?"

I gave you the correct valid answer using native VB commands.  Rejecting the correct answer and asking a different question is not really fair...

master01Author Commented:
You are right mcrider.  please just post any answer and you can have your points.  Sorry for the misunderstanding people.
Posting for points...

Thanks for playing fair!


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
master01Author Commented:
Sorry for the misunderstanding.
Thanks for the points!

Merry Christmas!

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.