Hi everyone,
I am trying to use SendMessage to send a WM_CHAR to a running instance
of Notepad.
So, in my simple VBA example, I am trying to send the character 'Q' to
the notepad and this should show this in the editor area, right?
So, I have the following API declarations:
WM_CHAR message
Public Const WM_CHAR = &H102
' Right control key
Public Const VK_RCONTROL = &HA3
' Q key
Public Const VK_Q = &H51
Private Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
As Long
I also have functions that can do wildcard comparisons for the
FindWindow
So, in my code I have the following:
Dim appHWnd As Long
appHWnd = FnFindWindowLike("*Note*")
If (appHWnd) Then
SendMessage appHWnd, WM_CHAR, VK_Q, ByVal 0&
Else
MsgBox "Could not find a running instance of "
End If
I see that the windo handle is returned and the SendMessage call gets
executed, but I do not see a 'Q' in the notepad window.
Does the window have to have the focus for SendMessage to work or have
I misunderstood this whole process.
Please help.
Thx,
Anja
Anja - 17 Dec 2006 15:18 GMT
> Hi everyone,
>
[quoted text clipped - 47 lines]
> Does the window have to have the focus for SendMessage to work or have
> I misunderstood this whole process.
Ok, I figured out that I had to use the WM_SETTEXT message.
Can someone tell me how I can use SendMessage to send the keystroke
(Alt F). Do I have to use 2 sendmessage calls?
Cheers,
Anja
> Please help.
>
> Thx,
> Anja
Douglas J. Steele - 17 Dec 2006 16:17 GMT
Why are you trying to automate Notepad?
Can you not simply read the data from the text file into a variable, do
whatever manipulations you need to with the text, and then write the data
back out (if need be)?
It's entirely possible that SendMessage will not work in the way you're
hoping, since Notepad doesn't expose itself for automation.

Signature
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)
> Hi everyone,
>
[quoted text clipped - 52 lines]
> Thx,
> Anja