Foxhound is the better* Database Monitor for SQL Anywhere.
*better: More thorough, more relevant, more effective.
...more Alerts, more All Clears, more details, more control in your hands.


Breck Carter
Last modified: July 24, 1996
mail to: bcarter@bcarter.com



Save As RTF

How do I save a DataWindow's contents in Rich Text Format?

The short answer is "You can't!", not even if the DataWindow's Presentation Style is set to Rich Text. The PowerBuilder 5 SaveAs() function doesn't support RTF as an output format.

The SaveAs function has another problem: Except for the new HTMLTable! SaveAsType the output only contains data from the buffer. It does not include computed fields and other formatting you've added to the DataWindow's visual representation. One might have wished Powersoft had given us an RTF! SaveAsType similar to HTMLTable! but they didn't.

What Powersoft did give us is the RichTextEdit window control plus the ability to share data between it and a DataWindow control. This is a first for PowerBuilder: A standard Windows control that works better than a DataWindow. As far as heavy-duty program control over RTF data is concerned we must stop repeating the mantra "Always use a DataWindow! Always use a DataWindow! Ommmm, ommmm..."

The way to save DataWindow data in RTF format is to use an ordinary DataWindow to retrieve the data, share it with a RichTextEdit control to add the formatting, and then save the contents of the RichTextEdit control. Here's what it looks like on the screen:

Figure 1: DataWindow and RichTextEdit Controls

Listing 1 shows what happens when the Setup button is pressed. A template file called province.rtf is loaded into the RichTextEdit control by a call to the InsertDocument function(). This template was created separately by another RichTextEdit control (not shown here) and a call to SaveDocument(). Unlike the DataWindow painter, PowerBuilder offers no way to paint the contents of a RichTextEdit control in the development environment. You must actually write and run a program, or use some other RTF-capable product.

Listing 1: cb_setup.clicked

   integer li_RC

   li_RC = parent.rte_province.InsertDocument &
      ( "province.rtf", true, FileTypeRichText! )
   if li_RC <> 1 then
      MessageBox ( "Error", "InsertDocument" )
      return
   end if

   li_RC = parent.rte_province.DataSource &
      ( dw_province )
   if li_RC <> 1 then
      MessageBox ( "Error", "DataSource" )
      return
   end if

The next step in Listing 1 is a call to DataSource() to share the data between the DataWindow and the RichTextEdit control. Why this function isn't called ShareData() is beyond me; perhaps that would be too easy to remember. After this code runs the display looks like Figure 1.

Listing 2 shows how to save the data via calls to SaveDocument(). Multiple calls (and multiple output files) are required when more than one DataWindow row is represented in the RichTextEdit control.

Listing 2: cb_save.clicked

   integer li_RC
   long    ll_row
   long    ll_return_row

   for ll_row = 1 to parent.dw_province.RowCount()

      // ScrollToRow returns row, not return code.

      ll_return_row = parent.rte_province.ScrollToRow &
         ( ll_row )
      if ll_return_row <> ll_row then
         MessageBox ( "Error", "ScrollToRow" )
         return
      end if

      // Save each rte_province "row" as a separate file.

      li_RC = parent.rte_province.SaveDocument &
         ( "prov" + string ( ll_row, "0000" ) + ".rtf", &
          FileTypeRichText! )
      if li_RC <> 1 then
         MessageBox ( "Error", "SaveDocument" )
         return
      end if

   next

If you think of the RichTextEdit control as working like a Freeform DataWindow then the calls to ScrollToRow() in Listing 2 will seem natural... except, that is, for the return value. Instead of the integer return code 1 or -1 returned by ScrollToRow for a DataWindow, the RichTextEdit ScrollToRow returns a long result containing the row number if it worked and -1 if it failed.

If you're wondering what RTF output looks like one of the files is shown in Figure 2.

Figure 2: Raw RTF Output

Folks who need to manipulate RTF data may find themselves using the RichTextEdit control instead of DataWindows with the Rich Text presentation style.


Breck Carter can be reached by phone at (416) 763-5200 or via email at bcarter@bcarter.com.