Output to a Word Document

The previous page demonstrated output of an array of records to a table in a csv file and in an rtf file. Here we show you how to install the fpVectorial component and to use it to output the same array of records to a table in a new .docx file. The code relies heavily on that provided on this wiki page.

  1. Select menu item Package > Install/Uninstall Package ... and select from the right panel fpvectorialpkg 0.0.
  2. Click the "Install selection" button and then the "Save and rebuild IDE" button and accept the invitation to continue.
    Lazarus will close and then reopen with the newly installed component.
  3. Select menu item Project > New Project and create a new simple program.
  4. Select menu item File > Save As ... and save the project as DocxDemo.
  5. Select menu item Project > Project Inspector and click on the Add (+) icon.
  6. In the Project Inspector, select the New Requirement tab and then select fpvectorialpkg from the drop-down list.
  7. Click on the "Create New Requirement" button and then paste this code to replace the existing code in the editor.
    program DocxDemo;
    
    uses
      fpvectorialpkg,  fpvectorial, fpvutils, {For RGBToFPColor} SysUtils;
    
    type
      TStudent = record
        Forename, Surname : string[15];
        TheoryMark, PracticalMark : integer;
      end;
    
    const
      MAX = 3;
    
    var
      Document: TvVectorialDocument;
      Page: TvTextPageSequence;
      Paragraph: TvParagraph;
      BoldTextStyle, CenterStyle: TvStyle;
      Table: TvTable;
      Row: TvTableRow;
      Cell: TvTableCell;
      iRow: Integer;
      Students : array[1 .. MAX] of TStudent;
    
    procedure NewCell;
    begin
      Cell := Row.AddCell;
      Paragraph := Cell.AddParagraph;
      Paragraph.Style := CenterStyle;
    end;
    
    begin
      // Hard code records for convenience
      with Students[1] do
        begin
          Forename := 'Jo';
          Surname := 'Wood';
          TheoryMark := 55;
          PracticalMark := 66;
        end;
      with Students[2] do
        begin
          Forename := 'John';
          Surname := 'Bode';
          TheoryMark := 73;
          PracticalMark := 58;
        end;
      with Students[3] do
        begin
          Forename := 'Kapil';
          Surname := 'Shah';
          TheoryMark := 59;
          PracticalMark := 58;
        end;
    
      Document := TvVectorialDocument.Create;
      try
        // Add the default paragraph styles StyleTextBody, StyleHeading1, StyleHeading2 & StyleHeading3
        Document.AddStandardTextDocumentStyles(vfUnknown);
        // Add style for emboldened text elements
        BoldTextStyle := Document.AddStyle;
        BoldTextStyle.Kind := vskTextSpan; // applied only to selected text spans
        BoldTextStyle.Name := 'Bold';
        BoldTextStyle.Font.Bold := True;
        BoldTextStyle.SetElements := BoldTextStyle.SetElements + [spbfFontBold];
        // Add style for centred paragraphs
        CenterStyle := Document.AddStyle;
        CenterStyle.Kind := vskTextBody; // applied to the whole paragraph
        CenterStyle.Name := 'Table Body Centered';
        CenterStyle.Font.Name := 'Verdana';
        CenterStyle.Font.Size := 9;
        CenterStyle.Alignment := vsaCenter;
        CenterStyle.MarginTop := 1;
        CenterStyle.MarginBottom := 1;
        CenterStyle.SetElements :=
          [spbfFontSize, spbfFontName, spbfAlignment, sseMarginTop, sseMarginBottom];
        // Create the document and add a simple heading
        Page := Document.AddTextPageSequence;
        Paragraph := Page.AddParagraph;
        Paragraph.Style := Document.StyleHeading3;
        Paragraph.AddText('Test Results');
        // Add table, which will have 4 columns
        Table := Page.AddTable;
        Table.PreferredWidth := Dimension(60, dimPercent);
        // Add 4 rows to the table, with the first being the header row
        for iRow := 0 to 3 do
          begin
            Row := Table.AddRow;
            if iRow = 0 then  // header
              begin
                Row.BackgroundColor := RGBToFPColor(192, 192, 192); // Grey shading
                Row.Header := True;
              end;
            NewCell;
            if iRow = 0 then
              Paragraph.AddText('Forename').Style := BoldTextStyle
            else
              Paragraph.AddText(Students[iRow].Forename);
            NewCell;
            if iRow = 0 then
              Paragraph.AddText('Surname').Style := BoldTextStyle
            else
              Paragraph.AddText(Students[iRow].Surname);
            NewCell;
            if iRow = 0 then
              Paragraph.AddText('Theory Mark').Style := BoldTextStyle
            else
              Paragraph.AddText(intToStr(Students[iRow].TheoryMark));
            NewCell;
            if iRow = 0 then
              Paragraph.AddText('Practical Mark').Style := BoldTextStyle
            else
              Paragraph.AddText(intToStr(Students[iRow].PracticalMark));
          end;
        Document.WriteToFile('TestResults.docx', vfDOCX);
      finally
        Document.Free;
      end;
    end. 
  8. Execute the program and open TestResults.docx (which should be in your program folder) with an application such as Microsoft Word, LibreOffice Writer or OpenOffice Writer.
    You should see this result:
    TestResults.docx viewed in LibreOffice Writer

    TestResults.docx viewed in LibreOffice Writer

Programming - a skill for life!

Input from keyboard and file and output to monitor, file and printer