Colour text in Text Box

JoséBob

New member
Local time
Tomorrow, 00:42
Joined
Sep 19, 2023
Messages
29
Hei,

I’d like to mark the difference between two text boxes.
I have the following code to open and compare the text but I don’t know how to change the colour of the letter/character which is checked different.
The .selcolor method and Selection.font.colorindex don’t work, the .forecolor isn’t suitable for this.
My textbox is in richtext and I have made use that the form is able to be modified. I checked and I can modify the font colour “by hand” using mouse.
In the end, the form will be opened in hidden mode to hide the macro from the user. Opening the form in hidden mode is the simplest solution I found. Maybe there is better way.
You'll find my current code herebelow.
Thanks for your support.

Bob

Code:
Public Function Compare()
Dim Text1 As String
Dim Text2 As String
Dim filterFrm As String
Dim i As Integer
i = 0
Set db = CurrentDb
PK = 101
filterFrm = "HistID=" & PK

DoCmd.OpenForm "HistList2", acNormal, , WhereCondition:="HistID=" & PK

Set TextBox2 = Forms.HistList2.Texte73
Set TextBox1 = Forms.HistList2!Texte52

Text1 = TextBox1.Value
Text2 = TextBox2.Value

'Compare text from two text boxes and set the colour to red :
For i = 1 To Len(Text2)
    If Mid(Text1, i, 1) <> Mid(Text2, i, 1) Then
    TextBox2.SetFocus
    TextBox2.SelStart = i - 1
    TextBox2.SelLength = 1
    TextBox2.Selcolor = RGB(255, 0, 0)
    End If
    Next i
    
DoCmd.Close acForm, "HistList2", acSaveYes

'Clean variable:
Set TextBox2 = Nothing
Set TextBox1 = Nothing
Set db = Nothing
End Function
 
This is done using rich text tags.
Here is a form that shows the same field, one in rich text and other control in plain text so you can see the tags.

RichText.jpg

When I color a part of text it gets color tags around it

RichText2.jpg


So in code you have to add the following tags around your selected text.
<font color = "#ED1c24> your selected text </font>
 
So you want to build a function where you can pass in a string and return it surrounded by tags. Here is an example where I pass in a string and create yellow highlight

Code:
Public Function MakeYellow(TextToColor As String) As String
  'Sets background shading yellow
  MakeYellow = "<div><font style='BACKGROUND-COLOR:#FFFF00'>" & TextToColor & "</font></div>"
End Function

There are some examples here
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom