Question: Have you ever created a formula, to build a string of text, and wanted to format parts of it separately?
Answer: Use HTML codes to format your string.
This Tech Tip uses the Customer Table from the Sample Database that comes with Crystal Reports.
The easiest way to format a text object is to right-click on it and select “Format Text…” from the context menu. Unfortunately, this only allows you to format all of the text the same way. You can make everything bold, change the font style and size, and underline it, etcetera. But, what if you want to only bold certain parts?
Well, you could make several text boxes and format them separately, but that is a lot of work and it doesn’t look nice. More advanced users may have noticed that you can double-click into the text object and format parts of text separately. You can even drag in other objects like summaries, database fields and special fields. These can all be formatted separately also. The problem here is that you can only change certain formatting once you drag the object into the text object. It will then be considered text and only simple text formatting options will be available like bold and size. So what happens if you summary rounded to two decimal places and later the report owner wants it changed to a whole number? You would have to create a new summary, drag it onto the report, format it and then replace the old one in the text object with the new one.
Now, let’s say you decided to build your text using a formula. Maybe you want to use functions to do your formatting such as Round() or ToText()? Maybe you just needed to build a complex formula to get the results of your text string. In any case, once you put your formula field on the report and try to format it, you are back to formatting changes that affect all of the text. Or, are you?
By incorporating a few HTML tags into your text string, you can give some parts your special touch. Here is an example of using a formula to print the Data Date in your page footer. Follow the steps and try for yourself:
Step 1: Create a new formula to display your text. In this example, we will name our formula “dtData Refreshed”. Like the example above, we will bold just the date and time. The formula for doing this is:
Step 2: Place the new formula field in the Page Footer.
Step 3: Right-click on the formula field in the report and select “Format Field…”
Step 4: Using the Paragraph tab, change the “Text interpretation” to HTML Text. As you may have noticed from our formula, you must surround your HTML tags with quotes so that they are a part of the text string. The end result of the formula will be all text and interpreted as HTML.
CaveatsNot all HTML tags are created equal. Most of the tags associated with text formatting (ex. bold, italics, font color/size/style) are supported. However, not all HTML tags are supported. As newer versions of Crystal Reports are released, more tags have been included. Please check the Crystal Reports support site for a listing of supported HTML tags for your version of Crystal. SAP Note 1217084 is a good place to start.
If you have ever used ChrW(10) or ChrW(13) to force a new line in your formula, you will need to use “<br>” instead. Yes, line breaks are supported. The sad part is that bulleted and numbered list are not supported in most versions.
So, how exactly did I get the red “Feb” in the date above? Well, that took a little more coding. Here is the code that I used. Of course, I could have simplified things using a variable, but instead, I just used cut-and-paste.