Solved

VB6 get text from richtextbox

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
Macro which automatically send Excel spreadsheet to Outlook problem 2 61
Run code from text file in vb 1 57
Copy a row 12 59
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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now