?
Solved

VB6 get text from richtextbox

Posted on 2004-09-01
3
Medium Priority
?
757 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:aonecomputers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Expert Comment

by:anv
ID: 11950867
can u select the contents of the RT box of other application...?

how do u manage to refer to that control when u dont have source of that application?

0
 
LVL 22

Accepted Solution

by:
DarkoLord earned 500 total points
ID: 11952292
Hi! Try this (credits to "Frans C")... (replace RichTextBox1.hwnd with the correct hwnd)

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_USER = &H400
Private Const EM_STREAMIN = (WM_USER + 73)
Private Const EM_STREAMOUT = (WM_USER + 74)
Private Const SF_TEXT = &H1
Private Const SF_RTF = &H2
Private Const SF_UNICODE = &H10         ' Unicode data of some kind

Private Type EDITSTREAM
    dwCookie As Long
    dwError As Long
    pfnCallback As Long
End Type

Private Sub Command1_Click()
Dim es As EDITSTREAM
Dim retval As Long
    es.dwCookie = 55 'put your own data in here, it is passed to the callback function
    es.pfnCallback = GetAddress(AddressOf EditStreamCallback)
    retval = SendMessage(RichTextBox1.hwnd, EM_STREAMOUT, SF_RTF, es)
End Sub


In a bas module:

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function EditStreamCallback(ByVal dwCookie As Long, ByVal pbBuff As Long, ByVal cb As Long, ByVal pcb As Long) As Long
Dim bArr() As Byte
Dim strBuff As String

    ReDim bArr(cb - 1)
    CopyMemory bArr(0), ByVal pbBuff, cb
    ' the data is in the byte array
    ' do with it what you want
    ' probably you want to save it to a file,
    ' but for the example I put it in a string
    strBuff = StrConv(bArr, vbUnicode)
End Function

Public Function GetAddress(ByVal lAddress As Long) As Long
    GetAddress = lAddress
End Function
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11956300
>>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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question