Microsoft KB Archive/250501

From BetaArchive Wiki

Article ID: 250501

Article Last Modified on 2/16/2005


  • 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


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 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()
    WITH oRange
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Font.Size = 14
        .Font.Name = "Arial Black"
        .InsertAfter("Heading for Sales Report")
        .Bold = .T.
        .Bold = .F.
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Font.Size = 12
        .Font.Name = "Times New Roman"
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .InsertAfter(REPLICATE("Paragraph #1 is left aligned. "+;
            "Paragraph 2 is justified. ",4))
        .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))
    *!* 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 object
    WITH oTable
        .Columns.SetWidth(72,0)               && 72 points/inch
        .Rows.WrapAroundText = .T.
        .Rows.RelativeHorizontalPosition = 0  && wdHorizontalPositionMargin
        .Rows.HorizontalPosition = -999996    && wdTableRight
    *!* (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(5,1).Range.InsertAfter("Quarterly Cookie "+;
            "Sales by Region - in $ Millions")
        .Cell(5,1).Range.Bold = .T.
        .Cell(5,1).FitText = .T.
        .Rows(1).Shading.Texture = 200  
    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)) 
        .InsertAfter("A New Paragraph")
    oWord.Visible = .T.  && Inspect the results
    MESSAGEBOX("Look at results in Word.")
    oWord.Quit(.F.) && Don't save changes
  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

Keywords: kbhowto kbautomation kbsample KB250501