Dim Data1 As String
        Dim Data2 As String
        Dim sFinal As String
        Dim sLeft As String
        Dim sRight As String
        Dim quote As String
        quote = """"

        Data1 = rtb.SelectedText 'original string
        Data2 = "<font color=" + quote + codenumber + quote + ">" 'string to insert

        sLeft = Microsoft.VisualBasic.Left(Data1, Len(Data1) - rtb.SelectionStart + rtb.SelectionLength) 'get the left section before insert
        sRight = Microsoft.VisualBasic.Right(Data1, rtb.SelectionLength) 'get right section after insert

        sFinal = sLeft & Data2 & sRight 'make new string of left & middle & right
        rtb.SelectedText = sFinal + "</font>"

I want to insert <font color="#fontcode"> before the selected text in rtb(richtextbox) and </font> at the end of the selected text. This code adds the <font....> for only 3 characters from right. Please let me know what mistake i am making. Thank you

Hi,
Your code to get the sections looks wrong, you should be passing in the whole rich text box text not data1 and I'm not sure you are calculating the point correctly i.e. sLeft = left(rtb.text, rtb.selectionstart-1) would get you the text in the rich text box just before the selection. sRight = mid(rtb.text,rtb.SelectionStart) would get you the text in the textbox from the selection start to the end of the textbox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.