Search code examples
visual-foxproword-automation

Tables got over-written


I want to loop thru a dbf and create word table for each record meeting the condition, and I got a one-page report with only the last rec in a single table. Look like all records are written to the same table. I tried to use n = n + 1 to place the variable as an element to the table oTable = oDoc.tables[n] But seems it only support numerical rather than variable ?


Solution

  • You have to add each table as you go, making sure to leave space in between them (because Word likes to combine tables).

    You'll need something like this inside your loop:

    * Assumes you start with oDoc pointing to the document,
    * oRange set to an empty range at the beginning of the area where you want to add the tables, 
    * and that nRows and nCols give you the size of the table.
    
    oTable = oDoc.Tables.Add(m.oRange, m.nRows, m.nCols)
    oRange = oTable.Range()
    oRange.Collapse(0)
    oRange.InsertParagraphAfter()
    oRange.Collapse(0)
    

    After this code, you can use oTable to add the data you want to add. Then, on the next time through the loop, you're ready to add another table below the one you just filled.