1
votes

I'm having trouble find a way to achieve this. Bascially, I just need a way to get the selected portion of text within a text box. The idea is that a user can double click a word within a larger string to automatically search against another set of data.

If can get the below to work, the selected text will simply call a function that runs my search process. The below does not work and many of my online finds only capture the whole text box. Any ideas?

Private Sub txtproductName_DblClick(Cancel As Integer)
    Debug.Print txtproductName.SelText
End Sub
2

2 Answers

2
votes

I've used MouseUp instead

Private Sub txtproductName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Len(txtproductName.SelText) > 0 Then
    'do my thing
    Debug.Print txtproductName.SelText
    Else
    'do nothing
    End If
End Sub
0
votes

You can use the LostFocus event.

Private Sub txtproductName_LostFocus()
    MsgBox Me.txtproductName.SelText
End Sub