Search code examples
crystal-reportsreportingstring-formattingtext-parsing

Formatting specific found words within a text string in Crystal Reports


I have a list of words that I am searching through a "Notes" field within a table, that when I display that Notes field in my Crystal Report, I would like to somehow highlight (change font color) for only the specific flagged word within the note text string.

Example: word list: Joe, Sarah, Amy, Jeff note text: "I stopped by and talked with Joe to check on the account status, and Amy said hello."

desired result: Note text displays in the report with the words "Joe" and "Amy" in red.

I've tried using RTF and HTML in the Text Interpretation parameter within the properties, where I can successfully format the note text to either of these text outputs. However, I still don't know the right code to isolate and format the specific words from my list, especially if more than one of my words shows up within the field text.

Thanks in advance for any help you can give me!


Solution

  • I did something similar a couple of years ago:

    StringVar SearchText := "has";
    StringVar Htm1 := "<b>";
    StringVar Htm2 := "</b>";
    StringVar Result := ""; 
    StringVar Temp := ""; 
    NumberVar Start := 1;  
    NumberVar Ln := Len(SearchText);  
    NumberVar Loc := Instr({@TextField}, SearchText);  
    While Loc > 0 Do (  
        Temp := Mid({@TextField}, Start, Loc - Start) + Htm1 + Mid({@TextField}, Loc, Ln) & Htm2;
        Result := Result + Temp;  
        Start := Loc + Ln;  
        Loc := Instr(Start, {@TextField}, SearchText);  
    );  
    Temp := Mid({@TextField}, Start);  
    Result := Result + Temp;  
    Result  
    

    In this case I am searching the field called @TextField for the value in SearchText and am bolding the values. Probably not the most efficient code, but it works.