Solved

VB6 get text from richtextbox

Posted on 2004-09-01
3
711 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
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 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now