Retrieve Data from a foreign process !

I want to retrieve and store in a string variable  the text in an open NotePad document .

How do I go about this ?

Who is Participating?
Paste this onto a form with a command button, open a notepad document and press the button.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_GETTEXT = &HD

Private Sub Command1_Click()
Dim nHwnd As Long
Dim nEditHwnd As Long
Dim nText As String * 256

nHwnd = FindWindow("Notepad", vbNullString)
nEditHwnd = FindWindowEx(nHwnd, 0&, "Edit", vbNullString)

SendMessageSTRING nEditHwnd, WM_GETTEXT, 256, nText

MsgBox nText

End Sub
RAFAAJAuthor Commented:

I can't believe I missed that !

I didn't want to try the sendmessage API because I just read that you can't read data in a foreign Memory space.

Apparently, this is what is read and is called InterProcess Marshalling which doesn't always work.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.