Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Hooking to a RichTextBox

Posted on 2004-03-24
Medium Priority
Last Modified: 2013-12-25
I wish to I hook onto a Rich Text Box, (RichEdit20 Class) control, from a VB application.
The designated text box, doesn't have to be on a Visual Basic application,
It's easy to get it's handle, but...
how can I get / set it's RTF?
Question by:SiCkness
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
  • 4
  • 2

Expert Comment

ID: 10874059
hi SiCkness ,, hope this helps
this is for getting/setting the normal text :( not the rtf but i am working on it :)
Please Inform me with any Questions or ideas ,,,


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 ' Used to send Msgs to the RTF Object
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long ' Used to get text from the RTF
Private Const WM_GETTEXT = &HD ' used here to get the textlength
Private Const EM_GETLINE = &HC4 ' Used to get the text of a specific Line in RTF
Private Const EM_GETLINECOUNT = &HBA ' used to get the Nr of lines in the RTF
Private Const EM_LINEINDEX = &HBB ' used to get the index of a specific line In RTF
Private Const EM_LINELENGTH = &HC1 ' Used to get the Line Length in RTF
Private Const WM_SETTEXT = &HC

Private Function GetRTFText(RTFHWND As Long, ReturnedLength As Long, ReturnedNrOfLines As Long) As String
' RTFHWND is the hwnd of the rtf object
' ReturnedLength is the identifier that will hold the length of the rtf
' ReturnedNrOfLines is the identifier that will hold the Nr of Lines of the rtf
 Dim RTFNrLines As Long ' Nr of lines
 Dim RTFLineIndex As Long ' Line Index
 Dim RTFTxtLength As Long 'the text length
 Dim RTFText As String 'all the text in the RTF
 Dim TmpBuffer As String 'a temporary buffer
 Dim XLoop As Integer ' the loop counter

'Nr of lines in the RTF Object
RTFNrLines = SendMessage(RTFHWND, EM_GETLINECOUNT, 0, 0)
ReturnedNrOfLines = RTFNrLines
'Length of all the text in the RTF
ReturnedLength = SendMessage(RTFHWND, WM_GETTEXT, 0, 0)

 For XLoop = 0 To RTFNrLines - 1
  RTFLineIndex = SendMessage(RTFHWND, EM_LINEINDEX, XLoop, 0)
  RTFLineLength = SendMessage(RTFHWND, EM_LINELENGTH, RTFLineIndex, 0)
  'recreate the Buffer to retrieve the text of the current Line
  TmpBuffer = Space(RTFLineLength)
  'Retrieve Text of current Line
   Call SendMessageStr(RTFHWND, EM_GETLINE, 0, ByVal TmpBuffer)
  RTFText = RTFText & TmpBuffer & vbCrLf
 Next XLoop

GetRTFText = RTFText ' Return the Text by the function
End Function

Private Sub Command1_Click()

'to get the text of the RTFBox:
Dim mytext As String
mytext = GetRTFText(RichTextBox1.HWND, 0, 0)
MsgBox mytext

'to clear the RTFTextBox :
Call SendMessageStr(RichTextBox1.HWND, WM_SETTEXT, 0, ByVal (""))
MsgBox "Text Cleared"

'to set the text of the RTFTextBox:
Call SendMessageStr(RichTextBox1.HWND, WM_SETTEXT, 0, ByVal ("Blablabla OHDev .... BlaBlaBla :)"))


Expert Comment

ID: 10879180
still interested ?

Author Comment

ID: 10879260
oh I'm interested, and cool, WM_GETTEXT is a new thing for me
thanks, everything if very clear... good job

any chance you figure out how to get the RTF format?

thanks again
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

OHDev2004 earned 1000 total points
ID: 10880472
yeah yeah ,, After a while fighting trough VB :)
Here it is Full functioning working code ,, lol

the previous comment was only to get the normal text not the RTF Code of it ,, after trying for a while i came up with this routine and yep it works :) but first i had to write it to a stream then to a file ,, then read it from a file because the previous comment was returning Plain text not RTF !

Here is the Full Code:

'In a Module
option explicit
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal RTFHwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long ' Used to send Msgs to the RTF Object
Private Declare Function WriteFile Lib "kernel32" (ByVal FileHndl As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Const WM_SETTEXT = &HC
Public Const WM_PASTE = &H302
Public Const WM_USER = &H400
Public Const EM_GETTEXTEX = (WM_USER + 94)
Public Const EM_STREAMOUT = (WM_USER + 74)
Public Const SF_TEXT = &H1
Public Const SF_RTF = &H2

Public Const OPEN_ALWAYS = 4
Const GENERIC_WRITE = &H40000000

 dwCookie As Long
 dwError As Long
 pfnCallback As Long
End Type

Public Function GetRTFText(RTFHwnd As Long, XType As String) As String
' RTFHwnd ---> The HWND of the RTFObject
' XType -----> The type of text to be retrieved ( "RTF" or "TXT")

Dim FileHndl As Long
Dim XFreeFile As Integer
Dim TBuffer As String, TmpStr As String
Dim TextType As Long ' Rtf or TXT

If XType = "RTF" Then 'to set the type of text to be retrieved
TextType = SF_RTF
TextType = SF_TEXT
End If

On Error Resume Next

FileHndl = CreateFile(App.path & "\Tmp1.txt", GENERIC_WRITE, 0&, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
 If FileHndl <> 0 Then
    RtfStream.dwCookie = FileHndl
    RtfStream.pfnCallback = Val(AddressOf SaveCallBack)
    RtfStream.dwError = 0&
       SendMessage RTFHwnd, EM_STREAMOUT, ByVal TextType, RtfStream
       CloseHandle FileHndl
    End If
XFreeFile = FreeFile
 Open App.path & "\Tmp1.txt" For Input As #XFreeFile
   While Not EOF(XFreeFile)
   Input #XFreeFile, TBuffer
    TmpStr = TmpStr & TBuffer & vbNewLine
    Close #XFreeFile
 GetRTFText = TmpStr
Call Kill(App.path & "\Tmp1.txt")
End Function

Public Function SaveCallBack(ByVal dwCookie As Long, pbBuff As Long, ByVal cb As Long, pcb As Long) As Long
    Dim retVal As Long
    On Error Resume Next
    retVal = WriteFile(dwCookie, pbBuff, cb, pcb, ByVal 0&)
    SaveCallBack = IIf(retVal <> 0, 0, Err.LastDllError)
End Function

'Examples :

Dim ReturnedText As String

'to get the text in RTF Format:
ReturnedText = GetRTFText(RichTextBox1.hWnd, "RTF")

'to get the text in PlainText Format:
ReturnedText = GetRTFText(RichTextBox1.hWnd, "TXT")

'to clear the RTFTextBox :
Call SendMessage(RichTextBox1.hWnd, WM_SETTEXT, 0, ByVal (""))

'to set RichText or Plaintext to the RTFTextBox:
Dim StrRtf As String
StrRtf = "{\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset0 MS Sans Serif;}}" & vbNewLine & "{\colortbl ;\red0\green128\blue0;}" & vbNewLine & "\viewkind4\uc1\pard\ltrpar\cf1\lang3073\f0\fs20 KPD-Team 1998\cf0\f1\fs17" & vbNewLine & "\par }"
'StrRtf = "Blablabla OHDev :) Blablabla"
Call SendMessage(RichTextBox1.hWnd, WM_SETTEXT, 0, ByVal (StrRtf))

Author Comment

ID: 10887502
Very Interesting ...
Thanks alot OHdev!
Just know I looked alot for this answer, numerus website and codes...
and your the only one who did it

Thanks again!

Expert Comment

ID: 10887675
Thanks SiCkness
I really enjoyed this one ,, you know with vb and some API's you can make magic :)
hope you don't use it in meany things ,, lol
Thanks again ! ,, OHDev

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

715 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