Microsoft KB Archive/250501

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

Article ID: 250501

Article Last Modified on 2/16/2005

-

APPLIES TO


 * 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 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Office Word 2003

-



This article was previously published under Q250501



SUMMARY
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
 Start Visual FoxPro. On the File menu, click New. From the File Type options list, select Form, and then click the New Form button. From the Form Controls ToolBox, add a Command button onto the new form. Double-click the new Command button to go to the code page for the Command Button click event.  Add the following code to the Click procedure of the Command button:
 * 1) DEFINE wdToggle 9999998
 * 2) DEFINE wdTableRight -999996
 * 3) DEFINE wdAlignParagraphLeft 0
 * 4) DEFINE wdAlignParagraphCenter 1
 * 5) DEFINE wdAlignParagraphJustify 3
 * 6) DEFINE wdHorizontalPositionMargin 0
 * 7) DEFINE wdCollapseEnd 0
 * 8) DEFINE wdCollapseStart 1
 * 9) DEFINE wdParagraph 4

oWord = CREATEOBJECT("Word.Application") oWord.Visible = .F. && Toggle this to True to see if there's                     && any difference oDoc = 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

oWord.ActiveDocument.Tables.Add(oRange, 5, 4, 1, 0) && Word 2000 syntax oTable = oWord.ActiveDocument.Tables(1) && Assign a table object WITH 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.) .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")
 * !* Need a table of 4 rows, 3 columns, plus cells for labels and headings.
 * !* Arguments are Range, #rows, #cols, [DefaultTableBehavior,] [AutoFitBehavior])
 * !* Word 97 syntax is oWord.ActiveDocument.Tables.Add(oRange, 5, 4)
 * !* (Format,ApplyBorders,ApplyShading,ApplyFont,ApplyColor,ApplyHeadingRows,
 * !*        ApplyLastRow,ApplyFirstColumn,ApplyLastColumn,AutoFit)

.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 ENDwith oRange = oTable.Range oRange.Collapse(wdCollapseEnd) && Move insertion point beyond table WITH 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") ENDwith

oWord.Visible = .T. && Inspect the results MESSAGEBOX("Look at results in Word.")

oWord.Quit(.F.) && Don't save changes ThisForm.Release  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.</ol>

<div class="references_section">