I am trying to get text from an AS400 session. I have tried many
different approaches from SendMessage to GetDlgItemText. I have
succeeded in using Edit Copy by customizing the keyboard in AS400 and
using API Keyboard events to copy the window to the clipboard and then
get the text using API clipboard calls. While this approach works I am
determined to avoid the windows clipboard and to get the text by some
other means that will avoid having to map the keyboard and use the
windows clipboard.

After searching and testing solutions found in the groups I decided to
ask for help. I have successfully used the code below to get text from
notepad but when trying it with Client Access AS400 window I get a
value of 1 for text length and null for text. I am able to get the
handle for the parent and the child window. I am more interested in
this as an exercise to understand how to use API calls to get the text
that is displayed in this window as my original code works well. This
understanding of how to retrieve text from this window will assist me
in getting text from other child windows that are inaccessible from the
edit copy command.

Thank you in advance.

Option Explicit
Private Declare Function GetDlgItemText Lib "user32" Alias
"GetDlgItemTextA" _
(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As
String, _
ByVal nMaxCount As Long) As Long
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 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 Const WM_GETTEXTLENGTH = &HE
Private Const WM_GETTEXT = &HD

Sub GetTextTest()
Dim mlngHandle As Long
Dim mlngRetVal As Long
Dim mstrBuf As String
mlngHandle = FindWindow("PCSWS:Main:00400000", vbNullString)
If mlngHandle <> 0 Then
mstrBuf = String(1024, 0)
MsgBox mstrBuf
mlngRetVal = GetDlgItemText(mlngHandle, 2, mstrBuf, 1023)
If mlngRetVal > 0 Then
mstrBuf = Left$(mstrBuf, mlngRetVal)
Debug.Print mstrBuf
End If
End If
End Sub

Sub GetTextTest2()
Dim mlngHandle As Long
Dim mlngRetVal As Long
Dim mstrText As String
'
mlngHandle = FindWindowEx(FindWindow("PCSWS:Main:00400000",
vbNullString), 0, "PCSWS:Pres:00400000", vbNullString) 'handle for
text window
mlngRetVal = SendMessage(mlngHandle, WM_GETTEXTLENGTH, 0&, ByVal
0&) + 1
Debug.Print mlngRetVal
'
If mlngRetVal > 0 Then 'there is text
mstrText = Space$(mlngRetVal)
Debug.Print mstrText
mlngRetVal = SendMessage(mlngHandle, WM_GETTEXT, mlngRetVal,
ByVal mstrText)
mstrText = Left(mstrText, mlngRetVal)
Debug.Print "-" & mstrText & "-"
MsgBox "-" & mstrText & "-"
End If
End Sub