Solved Colour text in Text Box

JoséBob

Member
Local time
Today, 20:46
Joined
Sep 19, 2023
Messages
43
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 & "");
 
Hi,
Coming back to close this thread.
In the end, I used the replace() to remove the html tags. I also used it to avoid to many of them when highligthing the changed word otherwise I would have had too many of them.
Again, I am not sure using the replace function was the right one, but it worked for me :
When the form is opened I can use a button to highlight the differences between the two text boxes. The macro runs like this:
1. it runs a replace() to remove all punctuation
2. then a split() to have two arrays containing only the words of each textboxes
3. then it'll compare each words of both arrays. If there's a difference, it'll replace inside the textboxes the word with the same word but between the html markers to allow an highlight.
4. it'll remove extra html markers.
I also had to manage the added and deleted words in order to avoid that the whole text box is marked although there is only one word added. This is done by comparing the word before or the word after.

Thanks to all for your help.
 

Users who are viewing this thread

Back
Top Bottom