Convert RTF / HTML Field to Text - Access 2007

Posted on 2009-02-17
Last Modified: 2013-11-27
I have a field in Access 2007 which is Rich Text and thus looks great within Access
However when I insert the data into Word via an Insert Database Field the codes come across with it
So I want to create a new field that is the TEXT version of the field for Mail Merge purposes

An Example of a field is as follows
<div>WSUS SP1</div>

<div>Running on argancap-fp</div>


I already have a function that converts most of the data but it leaves a lot of them and also doesn't translate Carriage Returns Properly
Code is as follows

Please advise what can convert a Rich Text properly

Thanks in advance

Function StripHTMLChars_V2(sStr)

  Dim iPos1 As Integer

    Dim iPos2 As Integer

    iPos1 = InStr(sStr, "<")

    Do While iPos1 > 0

        iPos2 = InStr(iPos1 + 1, sStr, ">")

        If iPos2 > 0 Then

            sStr = Left(sStr, iPos1 - 1) & Mid(sStr, iPos2 + 1)


            Exit Do

        End If

        iPos1 = InStr(sStr, "<")



    StripHTMLChars_V2 = sStr

End Function

Open in new window

Question by:Wizard_IT
    LVL 65

    Accepted Solution

    Do u have a richtextbox control available? You can use .text and .textrtf

    eg  create a new rich txtbox control, set the rtf text to be your rtf text, then use .text to get the text part only

    Function RemoveRTF(rtf)
        Dim ctlRtf As New RichTextBox
        ctlRtf.TextRTF = rtf
        RemoveRTF = ctlRtf.Text
    End Function


    Author Comment

    i'll try - looks simple enough
    Thanks - I shall try tomorrow as I'm tied up at the mo

    Author Comment


    Cannot use the above code - Have tried hanging it on a  form to test but cannot extract the text from the RTFObject - VB doesnt crash but an error along the lines of cannot reference is on the VBA screentip

    The type of text contains

    Do you think it best to work with a routine as above to strip out, baring in mind the blank lines !

    <div><font face=Arial size=2>administrator tal2007</font></div>

    <div><font face=Arial size=2>service-basilica tal2006 &nbsp;</font></div>

    <div><font face=Arial size=2>service-besadmin tal2006 </font></div>

    <div><font face=Arial size=2>service-backup tal2006 </font></div>

    <div><font face=Arial size=2>service-spam tal2006</font></div>

    <div><font face=Arial size=2>service-av tal2006 </font></div>

    <div><font face=Arial size=2>&nbsp;</font></div>


    The above displays the following on the screen
    administrator tal2007
    service-basilica tal2006  
    service-besadmin tal2006
    service-backup tal2006
    service-spam tal2006
    service-av tal2006


    Author Comment

    BTW - The password aren't valid if you thought I was NAIVE ;)
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Are we all aware that in Access 2007, "Rich Text" is actually HTML. (Not the true Rich Text format, rtf)?


    Author Comment

    I gathered that but wasn't 100% sure - hence the attached samples
    Thanks for confirmation
    LVL 65

    Expert Comment

    ok, so its html you want stripping. The example I gave was for rtf.

    Im not sure where the problem lies looking at the example you posted. Looks like its working?

    Can you give an example string that is messed up? i.e. what it looks like in word.

    Author Comment

    Hi Rockiroads
    Its been a while since I looked at this but its cropped up again in the same system
    I have tried your function but I am told teat the control in unavailable?
    Compile Error
    User-Defined Type Not Defined

    How can I get around this - Thanks

    Displaying the individual field objects on a report wors fine as Access deals with the formatting itself
    However I need to build a Large Text Box of multiple records, based on date oriders etc..........

    If I cant get this working I will need to include a Sub Report but then copying the data into Excel becomes an issue

    Thanks in advance again Rockiroads
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    So for the record, what was the final solution?
    LVL 9

    Expert Comment

    You can also use the Application.PlainText Method or PlainText function.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now