Visual FoxPro 6 and Microsoft Word Tables

Question:
How can I make a table in a Microsoft Word document through OLE Automation from VFP? I investigated with the Visual Basic editor and I can’t find how to do it. Could you give me more information?

Answer:
You add a table to a Word document by calling the Add() method of the Tables collection of the currently active document:

Function Add(Range As Range, NumRows As Long, NumColumns As Long) As Table

The Add method takes three parameters. The first parameter is a range where the table should be placed. The second parameter is the number of rows in the table. The third parameter is the number of columns in the table.

The Add method returns an object reference to the table so that you can manipulate the table. To insert a table at the current cursor position, you can pass the Range property of the Selection property.

Here is a code sample that opens Word, makes it visible, creates a blank document, adds a table with three rows and two columns, fills in the cells of the table, and then pauses to allow you to see what the document looks like:

loWord = CREATEOBJECT("word.application")loWord.visible = .T.loWord.Documents.Add()loWord.Selection.TypeText("What follows is a 3x2 table.")loTable = loWord.ActiveDocument.Tables.Add(loWord.Selection.Range,3,2)loTable.Cell(1,1).Range.Text = "R1C1"loTable.Cell(1,2).Range.Text = "R1C2"loTable.Cell(2,1).Range.Text = "R2C1"loTable.Cell(2,2).Range.Text = "R2C2"loTable.Cell(3,1).Range.Text = "R3C1"loTable.Cell(3,2).Range.Text = "R3C2"MESSAGEBOX("Pause so that you can see what was done")loWord.Quit()

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: