Changing forecolor of certain words in Rich Text Box

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
How might this done please? Have tried various methods without success e.g.

With RichTextBox1
.Text = "<font color="green"> Welcome to BLAHG BLAH. </font>"
.Select(richTextBox1.TextLength - 4, 4)
End With

The Only Excel-specific instructions I could find were for RTB on Worksheet whereas mine is on a Userform. And the to-be colored text is not selected. It's intended as a heading.
Thanks for any help.
 
Yes - see msg 9. Is something not clear? Not sure what you mean by 'rest of that' either.
It's be a big help if InkEdit would do that.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I was replying to msg 9. The unclear part is where you have "header". Header makes me think of column or field header/label/name.
If the following does not help perhaps mock up an example in a worksheet cell so that it is formatted the way you want. Then paste that as a pic so that the formatting is not altered. Then I can try to replicate it.
This is what I used to achieve the red and bold part:
VBA Code:
Private Sub InkEdit1_DblClick()

Debug.Print InkEdit1
With InkEdit1
    .Font.Size = 12
    .SelStart = 5
    .SelLength = 4
    .SelColor = vbRed
    .SelBold = True
    .SelLength = 0
End With

End Sub
I tried to use the afterupdate event of the control but it wouldn't fire. Perhaps that's because it's not linked/bound to anything?
 
Upvote 0
I think this will work, but haven't nailed it yet. A full description of what I'm trying to do is probably essential.
Initially there's a string like this
"p/Steve Sholes l/Radio Recorders Hollywood 19 Jan 1957 & 7 Sep 1957 e/Thorne Nogar"
Char before "/" is Key to designation (Header). I use this code to build the full text, (Very rough, can be refined later)
VBA Code:
Dim t As Variant, sess, i, xxx, Buildtext
Dim Job As String
sess = "p/Steve Sholes l/Radio Recorders Hollywood  19 Jan 1957 & 7 Sep 1957 e/Thorne Nogar"
Dim p, c, a
p = 1

Dim Col(2)
Col(0) = vbRed
Col(1) = vbBlue
Col(2) = vbGreen

Do
a = InStr(p, sess, "/")
If a = 0 Then Exit Do
c = c & a & "-"
p = a + 1
Loop
t = Split(c, "-")

For i = 0 To UBound(t) - 1
    Select Case Mid(sess, t(i) - 1, 1)
        Case "p"
            Job = "Producer:"
        Case "l"
            Job = "Location:"
        Case "e"
            Job = "Engineer:"
    End Select
    If Val(t(i + 1)) = 0 Then
    xxx = 100
    Else
    xxx = t(i + 1) - t(i) - 3
    End If
    Buildtext = Buildtext & Job & vbCrLf & Mid(sess, t(i) + 1, xxx) & vbCrLf & vbCrLf

Next
Me.InkEdit1.Text = Buildtext
Next is what I've found a bit tricky. I've uploaded what it should look like
This code isn't working yet but is what I'm experienting with

VBA Code:
Do
a = InStr(p, Buildtext, ":")
If a = 0 Then Exit Do
c = c & a & "-"
p = a + 1
Loop

Dim ss, sl
ss = 0
t = Split(c, "-")
With InkEdit1
For i = 0 To UBound(t) - 1
   ' sl = t(i)
    .SelStart = ss
    .SelLength = sl
    .SelBold = True
    .SelColor = Col(i): Debug.Print ss, sl
   ' If i = (UBound(t) - 1) Then Exit For
   ' ss = InStrRev(Buildtext, vbCrLf, t(i + 1)) - 2
   ' sl = 9: 'InStrRev(Buildtext, vbCrLf, t(i + 1))
Stop
Next
Coincidentally the sel length of each bolded bit is 9 but that won't always be the case.
Selstart does not match the instr position in Buildtext. Not sure why, perhaps the linefeeds.
Each ":"is at pos 9, 36, 102 and each Selstart should be 0,24,86 The remmed line are things tried to figure it out.

But the control is doing what I hoped. It is on a Userform and independant of the worksheet. What event you said was not firing may not be needed.
I do apologies for the b-awful code and hope I'm not pushing the limits here too much with all this.
 

Attachments

  • COL.jpg
    COL.jpg
    7.9 KB · Views: 2
Upvote 0
If someone else was to code this I think I'd benefit enormously. But this is slightly better
VBA Code:
Dim t As Variant
    Dim Job As String
    Dim Sess As String
    Dim i As Integer
    Dim xxx As Integer
    Dim BuildText As String
    Dim c As Variant
    Dim p As Integer
    Dim a As Integer
    Dim x As Integer
    Dim k As Integer
    Dim j As Integer

    Sess = "p/Steve Sholes l/Radio Recorders Hollywood  19 Jan 1957 & 7 Sep 1957 e/Thorne Nogar"
   
    Dim Col(3)
    Col(0) = vbRed
    Col(1) = vbBlue
    Col(2) = RGB(0, 192, 0)
   
    p = 1
    Do
        a = InStr(p, Sess, "/")
        If a = 0 Then Exit Do
        c = c & a & "-"
        p = a + 1
    Loop
   
    t = Split(c, "-")
    ReDim TheLengths(UBound(t) - 1)
    For i = 0 To UBound(t) - 1
        Select Case Mid(Sess, t(i) - 1, 1)
            Case "p"
                Job = "Producer:"
                 TheLengths(0) = Len(Job)
            Case "l"
                Job = "Recorded at:"
                 TheLengths(1) = Len(Job)
            Case "e"
                Job = "Engineer:"
                 TheLengths(2) = Len(Job)
        End Select
        If Val(t(i + 1)) = 0 Then
                xxx = 100
            Else
                xxx = t(i + 1) - t(i) - 3
        End If
        BuildText = BuildText & Job & vbCrLf & Mid(Sess, t(i) + 1, xxx) & vbCrLf & vbCrLf
   
    Next
    Me.InkEdit1.Text = BuildText
   
    p = 1
    c = ""
    Do
    a = InStr(p, BuildText, ":")
        If a = 0 Then Exit Do
        x = InStrRev(BuildText, vbCr, a)
        k = IIf(x = 0, 0, x + 1)
        c = c & k & "-"
        p = a + 1
    Loop
    c = Split(c, "-")
   
    With InkEdit1
        For i = 0 To UBound(t) - 1
            .SelStart = c(i) - j
            .SelLength = TheLengths(i)
            .SelBold = True
            .SelColor = Col(i)
            j = j + 3
        Next
    End With
 
Upvote 0
Your pic suggests to me that the words you want to bold/colour are constant. If that is the case I don't see the need for all of that. I would write a sub that takes parameters and call that with whatever event will work for you. The following is not fully tested. I added the colour parameter while posting so I'm not sure how to declare it. For now it is a string variable. The parameters would be like
VBA Code:
Sub formatFont(str As String, strColour As String)
Dim pos1 as Integer

pos1 = InStr(Me.InkEdit1, str)
If pos1 > 0 Then
    With InkEdit1
        .Font.Size = 10
        .SelStart = pos1 - 1
        .SelLength = 9
        .SelColor = strColour
        .SelBold = True
        .SelLength = 0
    End With
End If

End Sub

In the event you use, make 3 calls:
VBA Code:
formatFont "Producer:", vbRed
formatFont "Location:", vbBlue
formatFont "Engineer:", vbGreen
Your selection point is likely not correct because the Instr function returns 1 in your case, but the SelStart property is zero based. That's why I subtracted 1.
If your length is not always going to be 9 then add that as a parameter.

EDIT - the idea works if only used once but subsequent calls undo previous formatting. Will have to re-think that.
 
Last edited:
Upvote 0
This may not be suitable for your needs but it seems to work for me. I found that setting the font size inside the called function removed prior formatting, so I moved that to the first sub. Yes, the line breaks were being counted in the selection properties (2 characters, Chr(13) and Chr(10)) so the count was wrong. I've adjusted for that by adding an "offset" parameter as an integer.
VBA Code:
Private Sub InkEdit1_DblClick()

InkEdit1.Font.Size = 9
FormatFont "Producer:", vbRed, 1
FormatFont "Location:", vbBlue, 4
FormatFont "Engineer:", vbGreen, 7
End Sub

Sub FormatFont(str As String, strColour As String, i As Integer)
Dim pos1 As Integer

pos1 = InStr(Me.InkEdit1, str)
If pos1 > 0 Then
    With InkEdit1
        .SelStart = pos1 - i
        .SelLength = 9
        .SelColor = strColour
        .SelBold = True
        .SelLength = 0
    End With
End If

End Sub
1730133459088.png
 
Upvote 0
Many thanks @Micron. Very much improved and I can slot that into my stuff. So far all working spot on.
Apart from the ActiveX error when Excel opens... do you also get that ?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top