Add HTML tags to your rich text

Status
Not open for further replies.

Webbarr

New member
Local time
Today, 14:56
Joined
Mar 9, 2018
Messages
6
I've made a basic function that adds some HTML tags to my rich text boxes based on what I want to display to the user, E.G. red / bold text for warnings etc...

Below is the main function & some examples of how I use it.

It's not very sophisticated but it's served me well so far & I wanted to share it. If anyone has any suggestions for improvements would love to hear back!

Code:
Option Explicit
Public Enum udtHTMLTags
    bwBold = 1
    bwItalic = 2
    bwUnderline = 4
    bwLineBreak = 8
End Enum
Public Function fnAddHTML(strInput As String, Optional HtmlTags As udtHTMLTags, Optional strColour = vbNullString) As String
    Dim strStart As String, strEnd As String

    If HtmlTags And bwBold Then
        strStart = "<b>"
        strEnd = "</b>"
    End If

    If HtmlTags And bwItalic Then
        strStart = strStart & "<i>"
        strEnd = strEnd & "</i>"
    End If

    If HtmlTags And bwUnderline Then
        strStart = strStart & "<u>"
        strEnd = strEnd & "</u>"
    End If

    If strColour <> vbNullString Then
        strStart = strStart & "<font color=" & Chr(34) & strColour & Chr(34) & ">"
        strEnd = strEnd & "</font>"
    End If

    If HtmlTags And bwLineBreak Then
        fnAddHTML = strStart & strInput & strEnd & "<br>"
    Else
        fnAddHTML = strStart & strInput & strEnd
    End If
End Function


Public Function fnRedText(strWarningMessage As String) As String
    fnRedText = fnAddHTML("Warning: ", , "FF0000") & strWarningMessage
End Function


Public Sub PrintText()
    Dim strPrintText As String

    strPrintText = fnAddHTML("Some bold text", bwBold)
    Debug.Print strPrintText

    strPrintText = fnAddHTML("Some bold & underlined", bwBold + bwUnderline)
    Debug.Print strPrintText

    strPrintText = fnAddHTML("All the options", bwBold + bwItalic + bwUnderline + bwLineBreak, "00FF00")
    Debug.Print strPrintText
End Sub
 
Thanks for posting that! I didn't receive a notification that I'd had a reply, maybe I need to have a look at my account settings.

It's an interesting read!
 
FWIW, I've done a little experimentation along these lines, if anyone is still following this thread.



When you concatenate multiple text fields to make one longer one which is rich text, you can specify their fonts by bracketing them with the deprecated HTML
Code:
font
element. So for instance to concatenate the string
Code:
red text
and the string
Code:
black text
-- and make the two substrings the indicated colors -- you could do something like this:


Code:
Me.ctlRichText = "<font color='#ED1C24'>red text</font>" & "<font color='#000000'>black text</font>"


The way Access handles this is to wrap each of the differently formatted font elements in
Code:
div
elements, which is a nuisance. So what you've got to do then is to strip out the
Code:
div
tags (opening and closing), using something like the
Code:
Replace()
function:


Code:
Me.ctlRichText = Replace(Replace(Me.ctlRichText, "<div>", ""), "</div>", "")


...which results in:
red textblack text
It's very awkward, but it can be done!
 
I've only just started working with rich text fields in both the current app I'm working on in the estimates and my project system I updated that as found them very usefull


thanks for the info


mick
 
Status
Not open for further replies.

Users who are viewing this thread

Back
Top Bottom