How to automate Word from Visual FoxPro and add a table to a document

This article was previously published under Q250501
This article describes how to Automate Microsoft Word 97, Microsoft Word 2000, Microsoft Word 2002, or Microsoft Office Word 2003 from Microsoft Visual FoxPro 6.0 and later versions of Visual FoxPro to do the following:
  • Create a new document.
  • Add text and a table to the document.
  • Format the text.
  • Format the table.
  • Populate the table with data.

Steps to create a sample project

  1. Start Visual FoxPro.
  2. On the File menu, click New. From the File Type options list, select Form, and then click the New Form button.
  3. From the Form Controls ToolBox, add a Command button onto the new form.
  4. Double-click the new Command button to go to the code page for the Command Button click event.
  5. Add the following code to the Click procedure of the Command button:
    #DEFINE wdToggle 9999998#DEFINE wdTableRight -999996#DEFINE wdAlignParagraphLeft 0#DEFINE wdAlignParagraphCenter 1#DEFINE wdAlignParagraphJustify 3#DEFINE wdHorizontalPositionMargin 0#DEFINE wdCollapseEnd 0#DEFINE wdCollapseStart 1#DEFINE wdParagraph 4oWord = CREATEOBJECT("Word.Application")oWord.Visible = .F.  && Toggle this to True to see if there's                     && any differenceoDoc = oWord.Documents.Add()oRange = oDoc.Range()oRange.Collapse(wdCollapseStart)WITH oRange	.ParagraphFormat.Alignment = wdAlignParagraphCenter	.Font.Size = 14	.Font.Name = "Arial Black"	.InsertAfter("Heading for Sales Report")	.MoveEnd(wdParagraph,1)	.Bold = .T.	.Collapse(wdCollapseEnd)	.InsertParagraphAfter()	.MoveEnd(wdParagraph,1)	.Bold = .F.	.Collapse(wdCollapseEnd)	.ParagraphFormat.Alignment = wdAlignParagraphLeft	.Font.Size = 12	.Font.Name = "Times New Roman"	.InsertParagraphAfter()	.InsertParagraphAfter()	.ParagraphFormat.Alignment = wdAlignParagraphLeft	.InsertAfter(REPLICATE("Paragraph #1 is left aligned. "+;		"Paragraph 2 is justified. ",4))	.Collapse(wdCollapseEnd)	.InsertParagraphAfter()	.InsertParagraphAfter()	.Collapse(wdCollapseEnd)	.ParagraphFormat.Alignment = wdAlignParagraphJustify	.InsertAfter(REPLICATE("This is a long paragraph that "+;		"needs to wrap around a table that will fit in the "+;		"paragraph, aligned at the right margin. ", 3))	.Collapse(wdCollapseEnd)ENDwith*!* Need a table of 4 rows, 3 columns, plus cells for labels and headings.oWord.ActiveDocument.Tables.Add(oRange, 5, 4, 1, 0) && Word 2000 syntax*!* Arguments are Range, #rows, #cols, [DefaultTableBehavior,] [AutoFitBehavior])*!* Word 97 syntax is oWord.ActiveDocument.Tables.Add(oRange, 5, 4)oTable = oWord.ActiveDocument.Tables(1) && Assign a table objectWITH oTable	.Columns.SetWidth(72,0)               && 72 points/inch	.Rows.WrapAroundText = .T.	.Rows.RelativeHorizontalPosition = 0  && wdHorizontalPositionMargin	.Rows.HorizontalPosition = -999996    && wdTableRight	.Autoformat(2,.T.,.T.,.T.,.T.,.T.,.T.,.T.,.T.,.T.)*!* (Format,ApplyBorders,ApplyShading,ApplyFont,ApplyColor,ApplyHeadingRows,*!*         ApplyLastRow,ApplyFirstColumn,ApplyLastColumn,AutoFit)	.Cell(2,1).Range.InsertAfter("Qtr 1")	.Cell(3,1).Range.InsertAfter("Qtr 2")	.Cell(4,1).Range.InsertAfter("Qtr 3")	.Cell(1,2).Range.InsertAfter("Eastern")	.Cell(1,3).Range.InsertAfter("Central")	.Cell(1,4).Range.InsertAfter("Western")	.Cell(2,2).Range.InsertAfter("4.5")	.Cell(2,3).Range.InsertAfter("3.7")	.Cell(2,4).Range.InsertAfter("4.2")		.Cell(3,2).Range.InsertAfter("4.7")	.Cell(3,3).Range.InsertAfter("4.1")	.Cell(3,4).Range.InsertAfter("4.3")		.Cell(4,2).Range.InsertAfter("4.9")	.Cell(4,3).Range.InsertAfter("4.0")	.Cell(4,4).Range.InsertAfter("4.5")		.Rows(5).Cells.Merge	.Cell(5,1).Range.InsertAfter("Quarterly Cookie "+;		"Sales by Region - in $ Millions")	.Cell(5,1).Range.MoveEnd(wdParagraph,1)	.Cell(5,1).Range.Bold = .T.	.Cell(5,1).FitText = .T.		.Rows(1).Shading.Texture = 200	ENDwithoRange = oTable.RangeoRange.Collapse(wdCollapseEnd) && Move insertion point beyond tableWITH oRange	.InsertAfter("The table goes before this sentence. ")	.InsertAfter(REPLICATE("This is a long paragraph that "+;		"needs to wrap around a table that will fit in the "+;    	"paragraph, aligned at the right margin. ",5)) 	.InsertParagraphAfter()	.InsertParagraphAfter()	.InsertAfter("A New Paragraph")ENDwithoWord.Visible = .T.  && Inspect the resultsMESSAGEBOX("Look at results in Word.")oWord.Quit(.F.) && Don't save changesThisForm.Release					
  6. Run the program, and then click the Command button.

    When the message box that displays "Look at the results in Word." appears, activate Word to examine the results. When you have finished examining the results, minimize Word and click OK on the message box.
For additional information about the Microsoft Office (including Microsoft Word) Object Model and how to Automate, click the following article number to view the article in the Microsoft Knowledge Base:
222101 How to find and use Office object model documentation
160064 How to convert VBA to FoxPro for OLE automation
181926 How to automate mail merge to Word 97 SR-1 using OLE and ODBC
172847 How to cut and paste from general field into a Word document
171988 How to pass parameters to a Word 97 macro
176069 How to print a range of pages in Word using OLE automation
180901 How to create categorized table in Word 97 w/ OLE automation
240809 How to prevent Switch To or Application Busy message using OLE
245782 Find information on the Office 97/2000 application's object
198508 How to insert and format a picture in Word 97 w/ OLE automation
195404 How to keep Word print dialog box on top using APIs
175173 How to use Word to print general fields in Report Designer

Article ID: 250501 - Last Review: 02/16/2005 03:23:21 - Revision: 4.4

Microsoft Visual FoxPro 5.0 Standard Edition, Microsoft Visual FoxPro 5.0a, Microsoft Visual FoxPro 6.0 Professional Edition, Microsoft Visual FoxPro 7.0 Professional Edition, Microsoft Visual FoxPro 8.0 Professional Edition, Microsoft Visual FoxPro 9.0 Professional Edition, Microsoft Word 2002, Microsoft Word 2000, Microsoft Office Word 2003

  • kbautomation kbhowto kbsample KB250501