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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.