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

DATATypeDescription
oWordObjectWord Application OLE object
oDocObjectWord Document OLE object
cFileNameCharacterDefault output file name
lPreviewLogicalShow Word window with document on completion
nPageNumericCurrent page counter

Methods

MethodDescription
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
nOrientACCESS/ASSIGNGet/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

See Also