Put this in a blank form, open up notepad, type something in, then run!
Also works for password textboxes!
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As _
Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
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.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private m_Buffer As String
Private m_length As Long
Private notepad_hwnd As Long
Private edit_hwnd As Long
Private Sub Form_Load()
notepad_hwnd = FindWindow("notepad", vbNullString)
edit_hwnd = FindWindowEx(notepad_hwnd, 0, "Edit", vbNullString)
'Get the handle to the edit box
'---- Structure found using Spy++ - wonderful program! ----
m_length = SendMessage(edit_hwnd, WM_GETTEXTLENGTH, 0, 0) + 1
'Get the text length
m_Buffer = Space$(m_length) 'Dimension our string to the text length
If SendMessage(edit_hwnd, WM_GETTEXT, _
Len(m_Buffer), ByVal m_Buffer) Then
'Actually get the text
MsgBox TrimNull(m_Buffer) 'The text we retrieved
End If
End Sub
Private Function TrimNull(ByVal StrIn As String) As String
Dim nul As Long
'Trims null strings
nul = InStr(StrIn, vbNullChar)
Select Case nul
Case Is > 1
TrimNull = Left$(StrIn, nul - 1)
Case 1
TrimNull = ""
Case 0
TrimNull = Trim$(StrIn)
End Select
End Function