Change text that select in a text box

M

Marco Silva

Hello.

I want to make some changes in the selected text in a text box.

Imagine that I have this text in a text box:
"The Web Server may be down, too busy, or experiencing other problems
preventing it from responding to requests. You may wish to try again at a
later time. "

Imagine that I select some part of text and in the selected area I want to
add the folowing codes: <b> </b>

In the example, imagine that I select "Web Server", I want to press a button
and add the codes to that text in order to get this:

"The <b>Web Server</b> may be down, too busy, or experiencing other problems
preventing it from responding to requests. You may wish to try again at a
later time. "

Please help,
Marco
 
B

Brendan Reynolds

Marco Silva said:
Hello.

I want to make some changes in the selected text in a text box.

Imagine that I have this text in a text box:
"The Web Server may be down, too busy, or experiencing other problems
preventing it from responding to requests. You may wish to try again at a
later time. "

Imagine that I select some part of text and in the selected area I want to
add the folowing codes: <b> </b>

In the example, imagine that I select "Web Server", I want to press a
button
and add the codes to that text in order to get this:

"The <b>Web Server</b> may be down, too busy, or experiencing other
problems
preventing it from responding to requests. You may wish to try again at a
later time. "

I've left in a few Debug statements that I used while testing, as they may
help to clarify what's happening, you can delete or comment out the lines
begining with "Debug".

This is quick and dirty example code, it needs cleaning up and and enhancing
for example to handle the case where the text field may be empty or the user
might not have selected any text.

Option Compare Database
Option Explicit

Private mlngStartOfSelection As Long
Private mlngLengthOfSelection As Long
Private mlngEndOfSelection As Long

Private Sub Command1_Click()

Dim strBeforeSelection As String
Dim strInSelection As String
Dim strAfterSelection As String

strBeforeSelection = Left$(Me.testtext, mlngStartOfSelection)
Debug.Print "|"; strBeforeSelection; "|"

strInSelection = Mid$(Me.testtext, mlngStartOfSelection + 1,
mlngLengthOfSelection)
Debug.Print "|"; strInSelection; "|"

strAfterSelection = Mid$(Me.testtext, mlngEndOfSelection + 1)
Debug.Print "|"; strAfterSelection; "|"

Me.testtext = strBeforeSelection & "<b>" & strInSelection & "</b>" &
strAfterSelection

End Sub

Private Sub testtext_LostFocus()

mlngStartOfSelection = Me.testtext.SelStart
mlngLengthOfSelection = Me.testtext.SelLength
mlngEndOfSelection = mlngStartOfSelection + mlngLengthOfSelection

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top