I am sorry if this is considered as cross-posting but this question was in an inadequate part of the forum and I feel this would be better answered here.
I need to remove some formatting from a rich text box (namely the font size and font face). I found a solution on stackoverflow dating from 4 years ago (here:https://stackoverflow.com/questions/...ze-not-working)
Using the VBA code I was able to create a module and button on my form to clean up the code.
I've been using this code for the past month without issue until yesterday. I noticed that when I have part of my text set to a different colour the code seems to be breaking after is passes over the coloured text.
Here's the code for the module:
Code:
Public Function CleanRichText(strTEXT, strFont, nSize)
For i = 1 To 9
strTEXT = Replace(strTEXT, "size=" & i, "size=" & nSize)
Next i
strTEXT = Replace(strTEXT, "font face", "font_face")
strTEXT = Replace(strTEXT, "font" & Chr(13) & Chr(10) & "face", "font_face")
Do While InStr(1, strTEXT, "font_face=" & Chr(34)) > 0
iCut1 = InStr(1, strTEXT, "font_face=" & Chr(34))
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(34))
strLeft = Left(strTEXT, iCut1 - 1) & "font_face=Face"
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
Do While InStr(1, strTEXT, "font_face=") > 0
iCut1 = InStr(1, strTEXT, "font_face=")
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(32))
strLeft = Left(strTEXT, iCut1 - 1) & "font face=" & strFont & Chr(32)
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
CleanRichText = strTEXT
And the button:
Code:
Private Sub CleanTextBox_Click()
MsgBox ("Updating the comments to Arial 11pts")
Me.NOTES = CleanRichText(Me.NOTES, Me.NOTES.FontName, 2)
End Sub
Here is an example of how my unclean (raw) text looks like when it has colour in it:
Code:
<div><font face="Times New Roman" size=3>So basically any sentence</font><font face="Times New Roman" size=3 color=red>with </font><font face="Times New Roman" size=3>a colour in it will break?</font></div>
And here this is what happens right after clicking the button:
Code:
<div><font face=Arial size=2>So basically any sentence </font><font face=Arial size=2 color=red>with </font><font face=Arial color in it will break?</font></div>
It looks like when the module is run against the colour it doesn't close.
The issue is only occurring with colour and never with other type of custom formatting. I'm not an expert in VBA but my understanding of the code is that the module will go over the code, look for and then for face and size will remove anything between the " " so it can default back to my formatting. Since color also take place within the part, I think this is what's causing the not closing tag.
Any idea on how to add a line of code to make sure that whatever is within color="" wont be affected?
Thanks