Get The Currently Selected Text In Rich Text Box

Preparations

Add 1 Command Button (named Command1), and 1 Rich Text Box (named RichTextBox1) to your form.

Module Code

Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As _
Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Public Const EM_GETSELTEXT = &H43E

Form Code

Private Sub Command1_Click()
    Dim selectedTxt As String
    Dim
lRet As Long
   
selectedTxt = Space(1000)
    lRet = SendMessageStr(RichTextBox1.hwnd, EM_GETSELTEXT, 0, ByVal selectedTxt)
    MsgBox "The selected Text: " & selectedTxt
End Sub

Go Back