Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Using MS Word as report generator

Title: Using MS Word as report generator. Question: How to use MS Word as report generator? Answer: Why not use the MS Word as report generator in your projects? We can easyly build the report and allow user to modify it using well known editor in any way he wants. The example below demonstartes how to build the report based on StringGrid contents. procedure TsiGridReporter.ShowReport; var Range: Variant; i, j: integer; begin if FGrid = nil then raise Exception.Create('No grid selected!'); try FWordApp := CreateOleObject('Word.Application'); except raise Exception.Create('Cannot start MS Word!'); end; FWordApp.Visible := True; FWordApp.Documents.Add; if FShowDate then begin Range := FWordApp.Documents.Item(1); Range := Range.Sections.Item(1); Range := Range.Headers.Item(1).Range; Range.Text := 'Date: ' + DateToStr(Date) + ' Time: ' + TimeToStr(Time); end; Range := FWordApp.Documents.Item(1); Range := Range.Sections.Item(1); Range := Range.Footers.Item(1); Range.Range.Text := 'Page:'; Range.Range.ParagraphFormat.Alignment := ord(waAlignParagraphRight); Range.PageNumbers.Add; FWordApp.Documents.Item(1).Paragraphs.Add; Range := FWordApp.Documents.Item(1).Range( FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End, FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End); Range.Text := FTitle; Range.Bold := fsBold in FTitleFont.Style; Range.Italic := fsItalic in FTitleFont.Style; Range.Underline := fsUnderline in FTitleFont.Style; Range.Font.StrikeThrough := fsStrikeOut in FTitleFont.Style; Range.Font.Name := FTitleFont.Name; Range.Font.Size := FTitleFont.Size; Range.Font.ColorIndex := ord(FTitleColor); Range.ParagraphFormat.Alignment := ord(FTitleAlignment); FWordApp.Documents.Item(1).Paragraphs.Add; FWordApp.Documents.Item(1).Paragraphs.Add; Range := FWordApp.Documents.Item(1).Range( FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End, FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End); FWordApp.Documents.Item(1).Tables.Add(Range, FGrid.RowCount, FGrid.ColCount); Range := FWordApp.Documents.Item(1).Tables.Item(FWordApp.Documents.Item(1).Tables.Count); for i := 1 to FGrid.RowCount do for j := 1 to FGrid.ColCount do begin Range.Cell(i, j).Range.InsertAfter(FGrid.Cells[j-1, i-1]); if (i Range.Cell(i, j).Range.Bold := True; Range.Cell(i, j).Range.Shading.BackgroundPatternColorIndex := ord(wcGray25); end else begin Range.Cell(i, j).Range.Bold := fsBold in FCellFont.Style; Range.Cell(i, j).Range.Italic := fsItalic in FCellFont.Style; Range.Cell(i, j).Range.Underline := fsUnderline in FCellFont.Style; Range.Cell(i, j).Range.Font.StrikeThrough := fsStrikeOut in FCellFont.Style; Range.Cell(i, j).Range.Font.Name := FCellFont.Name; Range.Cell(i, j).Range.Font.Size := FCellFont.Size; // Range.Cell(i, j).Range.Font.ColorIndex := ord(FCellColor); Range.Cell(i, j).Range.Shading.BackgroundPatternColorIndex := FCellColor; end; end; end; This example is just one method of component attached to this article. This component also has the PrintReport and PrintPreview methods. See attached source code for details. This component could give you just the first step for creating your own full featured report generator based on using MS Word. P.S. Component and source code are FREEWARE, so you can use it as you want.