aonecomputers
asked on
VB6 get text from richtextbox
Can anyone give me a sample of VB6 code to get plaintext from a richtextbox in another application (which I do not have the source) into a buffer in my program. I then have to search this buffer for certain phrases.
I have used Sendmessage(hwnd, WM_GETTEXT, length, strtmp) which works for an hour or so but eventually crashes probably because the lenght is way over 64K and usually over 100k.
I see that I am supposed to use Sendmessage with EM_STREAMOUT. I haven't been able to get this to work and all the examples I can find are in C.
Thanks for any help.
I have used Sendmessage(hwnd, WM_GETTEXT, length, strtmp) which works for an hour or so but eventually crashes probably because the lenght is way over 64K and usually over 100k.
I see that I am supposed to use Sendmessage with EM_STREAMOUT. I haven't been able to get this to work and all the examples I can find are in C.
Thanks for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>but eventually crashes probably because the lenght is way over 64K and usually over 100k.
It shouldn't crash.. a string can contain about 2 billion characters (about 1 billion if in Unicode).
Form1:
-------------
Option Explicit
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 Sub Command1_Click()
Dim strBuffer As String
If WindowGetText(RichTextBox1 .hwnd, strBuffer) = True Then
'Just MsgBox the first 10 characters...
Call MsgBox(Left$(strBuffer, 10) & "...", , "Length: " & Len(strBuffer))
End If
End Sub
Private Sub Form_Load()
'Fill RichTextBox with A LOT of text (may take a while)...
RichTextBox1.Text = String(1000000, "0")
End Sub
Private Function WindowGetText(ByVal lngHandle As Long, ByRef strBuffer As String) As Boolean
Dim lngLen As Long
lngLen = SendMessage(lngHandle, WM_GETTEXTLENGTH, 0, 0)
If lngLen > 0 Then
strBuffer = String(lngLen, vbNullChar)
If SendMessage(lngHandle, WM_GETTEXT, lngLen, ByVal strBuffer) = 0 Then
WindowGetText = True
End If
End If
End Function
It shouldn't crash.. a string can contain about 2 billion characters (about 1 billion if in Unicode).
Form1:
-------------
Option Explicit
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 Sub Command1_Click()
Dim strBuffer As String
If WindowGetText(RichTextBox1
'Just MsgBox the first 10 characters...
Call MsgBox(Left$(strBuffer, 10) & "...", , "Length: " & Len(strBuffer))
End If
End Sub
Private Sub Form_Load()
'Fill RichTextBox with A LOT of text (may take a while)...
RichTextBox1.Text = String(1000000, "0")
End Sub
Private Function WindowGetText(ByVal lngHandle As Long, ByRef strBuffer As String) As Boolean
Dim lngLen As Long
lngLen = SendMessage(lngHandle, WM_GETTEXTLENGTH, 0, 0)
If lngLen > 0 Then
strBuffer = String(lngLen, vbNullChar)
If SendMessage(lngHandle, WM_GETTEXT, lngLen, ByVal strBuffer) = 0 Then
WindowGetText = True
End If
End If
End Function
how do u manage to refer to that control when u dont have source of that application?