FWMSWordDoc
Source: source/classes/fwmsword.prg
FWMSWordDoc generates Microsoft Word documents programmatically via OLE Automation. It wraps the Word object model to create, format, and save documents with text, tables, images, shapes, and page layout control. The class provides a TPrinter-compatible API surface, allowing print-oriented code to produce Word output with minimal changes.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
oWord | Object | Word Application OLE object |
oDoc | Object | Word Document OLE object |
cFileName | Character | Default output file name |
lPreview | Logical | Show Word window with document on completion |
nPage | Numeric | Current page counter |
Methods
| Method | Description | |
|---|---|---|
New( cFileName ) | Create a new Word document via OLE; return nil if Word is unavailable | |
End() | Save if a filename was provided, optionally show Word window, then close | |
Save( cFileName, lPdf ) | Save document to file; supports docx, doc, and PDF formats | |
SetPage( nPage ) | Set the paper size using DMPAPER_ constants | |
StartPage() | Insert a page break and advance the page counter | |
EndPage() | Placeholder for printer compatibility (no-op) | |
Line( nTop, nLeft, nBottom, nRight, oPen, cUnits ) | Draw a line shape in the document | |
Box( nTop, nLeft, nBottom, nRight, oPen, oBrush, aText, cUnits, nShape ) | Draw a rectangle, rounded rectangle, or oval shape | |
Say( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad, cUnits ) | Insert formatted text at a position | |
SayText( nRow, nCol, cText, nWidth, nHeight, oFont, cAlign, nClrText, nClrBack, cUnits ) | Insert text in a text box with full alignment control | |
PrintImage( nRow, nCol, uImage, nWidth, nHeight, lStretch, nAlpha, lTransp, lGray, cUnits, cAlign ) | Insert an image from file or memory | |
nOrient | ACCESS/ASSIGN | Get/set page orientation (1 = portrait, 2 = landscape) |
SetPortrait() | Switch page to portrait orientation | |
SetLandscape() | Switch page to landscape orientation |
Example: Generate Word Document with Table
#include "FiveWin.ch"
function Main()
local oWord
oWord := FWMSWordDoc():New( "report.docx" )
if oWord == nil
MsgStop( "Microsoft Word is not available" )
return nil
endif
oWord:Say( 1, 1, "Sales Report", oWord:oWord:Selection:Font,;
nil, 0, nil, TA_CENTER )
oWord:Line( 2, 1, 2, 100, 1, "CM" )
oWord:Box( 4, 1, 8, 100, 1, RGB( 200, 200, 255 ), , "CM" )
oWord:PrintImage( 10, 1, "logo.bmp", 40, 20, .T., , , , "MM" )
oWord:End()
return nil
Notes
- FWMSWordDoc requires Microsoft Word to be installed on the target machine. The
New()constructor returnsnilif the OLE object cannot be created. - The class is designed with a TPrinter-compatible API:
Say(),Line(),Box(),PrintImage(), and unit conversion methods mirror the TPrinter interface for easy migration. - Coordinate units are managed via
Units2Pix()/Pix2Units()which accept "PIXEL", "INCH", "CM", or "MM". - The
Save()method can export to PDF format when Word 2007 or later (version 12.0+) is used. - When
lPreviewis.T.(the default), the Word window becomes visible afterEnd(), allowing the user to review the document.