using SendMessage with notepad

A

Anja

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
 
A

Anja

Anja said:
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.

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
 
D

Douglas J. Steele

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top