Colour text in Text Box (1 Viewer)

JoséBob

Member
Local time
Today, 05:19
Joined
Sep 19, 2023
Messages
32
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:
Hei.
Thanks for you reply. It is indeed useful.

btw, I tried highlighting words instead of letters. I use the split function and compare words one by one highlighting those which aren't identical.
let's see if I succeed.
 
Last edited:
You may find my example app useful:

1731618022621.png
 
Thanks to Isladogs for the link to the detailed site.
Can I use the replace() to remove the rich text tags? to clear all formatting. i tried but it seems the replace doesn't find the tags.
 
Use the PlainText Function to remove all rich text formatting
 
Using plain text is, in my case, a little bit brutal. I need to keep the line breaks (marked automatically with <div> and </div>), thus only removing the formatting <font style=blabla> </font>.
 
chatgpt has a suggestion to remove the <font...> </font> tag:
Code:
Public Function RemoveFontTags(richText As String) As String
    Dim regEx As Object
    Set regEx = CreateObject("VBScript.RegExp")
   
    ' Configure the regular expression
    regEx.Global = True
    regEx.IgnoreCase = True
   
    ' Pattern to match opening <font> tags with attributes
    regEx.Pattern = "<font[^>]*>"
    richText = regEx.Replace(richText, "")
   
    ' Pattern to match closing </font> tags
    regEx.Pattern = "</font>"
    richText = regEx.Replace(richText, "")
   
    ' Return the cleaned-up text
    RemoveFontTags = richText
End Function

using a Query to update your Richtext field:

Update YourTable Set YourRichField = RemoveFontTags(YourRichField & "");
 

Users who are viewing this thread

Back
Top Bottom