INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

Inter-Process Communication

How do I send keystrokes to Notepad/Wordpad? by chaitu
Posted: 4 Jul 01

Here is a simple but effective code used to send keystrokes
to Notepad / Wordpad.

Sending text to another program
------------------------------------------------------------

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Const WM_SETTEXT = 12

Public Sub ToNotePad(x As String)
    'assuming notepad is open
    Dim h As Long, ed As Long, t As Long
    h = FindWindow("Notepad", "Untitled - Notepad")
    If (h <> 0) Then
        ed = FindWindowEx(h, 0, "Edit", "")
        If (ed <> 0) Then
            SendMessage ed, WM_SETTEXT, ByVal 0, ByVal x
        Else
            MsgBox "Could not find notepad's edit"
        End If
    Else
        MsgBox "Could not find notepad"
    End If
End Sub



And for WordPad:



Public Sub ToWordPad(x As String)
    'assuming wordpad is open
    Dim h As Long, ed As Long, t As Long
    h = FindWindow("WordPadClass", "Document - WordPad")
    If (h <> 0) Then
        ed = FindWindowEx(h, 0, "RichEdit20A", "")
        If (ed <> 0) Then
            SendMessage ed, WM_SETTEXT, ByVal 0, ByVal x
        Else
            MsgBox "Could not find wordpad's richedit"
        End If
    Else
        MsgBox "Could not find wordpad"
    End If
End Sub

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close