Hello there, how can I SendKeys to the same window with out having to run a new instance of it every time I click on the command button on my form ?

Private Sub Command1_Click()

Shell "notepad", vbNormalFocus

SendKeys "This is a test string"

End Sub

The code above keeps opening a new notepad window every time I click on the command button, I wish to keep sending different text to the same window when ever I wish, with out running a new instance of the notepad excutable, how can I accomplish this in Visual Basic 5 please ?

Cheers MagicBytes

check out this sample code :-

put a textbox(Text1) and a command button(Command1) on your form before u execute the following code. in the code, here "file1.txt" is the target file which i'm creating and sending different text at different time but instead of opening a new instance its closing the currently opened instance and opening a new notepad window.if u wish to modify the code,just change the name of the text file in the section where u find the term "file1.txt" and put ur filename.

Option Explicit

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

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10

Private Sub Command1_Click()
Dim winHwnd As Long, RetVal As Long

If Dir(App.Path & "\file1.txt") = "" Then
    Open App.Path & "\file1.txt" For Output As #1
        Print #1, Text1.Text
    Close #1
    Shell "notepad.exe " & App.Path & "\file1.txt", vbNormalFocus
Else
    winHwnd = FindWindow(vbNullString, "file1 - Notepad")
    If winHwnd <> 0 Then
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
    End If
    Kill App.Path & "\file1.txt"
    Open App.Path & "\file1.txt" For Output As #1
        Print #1, Text1.Text
    Close #1
    Shell "notepad.exe " & App.Path & "\file1.txt", vbNormalFocus
End If
End Sub

Hello, thank you for your reply time and effort reading and supplying the code, but this is not what I have in mind for my project.

I really need the SendKeys command in my project as this command sends key strokes to which ever app a user chooses, the problem is , it seems as thought SendKeys will only send key strokes to an app in conjunction with the shell command, I have toyed withe the SendKeys command trying different things with out any luck, this is why I decide to come here hoping some body else is experiencing the same issues.

I have searched googles extensively looking for the answer and I have not found anything on this topic.

But there must be a VB command that I can use with the SendKeys command.

I was thinking about a command that recognises that an application is currently open and captures that windows title bar caption and then SendKeys to this window, for example Untitled - notepad.

I played with the AppActivate command and it did put the focus on the Untitled- notepad, but when VB gets to the sendkeys command no keys strokes appear on the notepad.

for example:

Private Sub Button1_click()

AppActivate "Untitled - Notepad"
SendKeys "This is a Test !", True

End Sub


But the example code only gives the Untitled - Notepad window the focus and no text appears in the file or window.

I keep clicking Button1 button and I see the Untitled-notepad flash, but no text is sent.

Is there more commands I need to accomplish what I wish to do ?


Cheers MagicBytes

The VB shell-command returns an ID that you can use to activate the specified application.

Dim MyAppID as Long

Sub StartApplication() 
    MyAppID = Shell("C:\Notepad.exe", 1)
End Sub

Sub MySendkeys(String Keystring, Optional Boolean Wait=False)
    If (MyAppID <> 0) Then    
        Call AppActivate(MyAppID)
        Call SendKeys(Keystring, Wait)
    End If
End Sub

This should do the trick.

Dennis

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.