Jump to content

Recommended Posts

Posted

I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.

Here's my early attempt:

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, $sFile)
Local $aData = _Word_DocTableRead($oDoc, 1)

$aData[3][5] = "Something else"

Local $oRange = _Word_DocRangeSet($oDoc, 0)
$oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9)

_Word_DocTableWrite($oRange,$aData)

This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?

 

Posted (edited)

As the new table is of different size I suggest to delete the existing one and write the new table at the same position.
This should remove the table.

$oDoc.Tables(1).Delete

 

Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 8/13/2018 at 11:01 PM, water said:

As the new table is of different siz I suggest to delete the existing one and write the new table at the same position.
This should remove the table.

Expand  

There are color, font, and table width settings that I would like to keep. Would I just be better off rebuilding the table from scratch and setting these things after the fact?

Posted

If you want to keep the table then I would just add the required number of rows so the table and the array have the same number of rows and columns and then loop through the array and move each cell to the corresponding cell in the table:

$oDoc.Tables(1).Rows.Add($oDoc.Tables(1).Rows(1)) ; Adds an empty row before the first row
For $iRow = 0 to UBound($aData, 1) - 1
    For $iColumn = 0 to UBound($aData, 1) - 1
        $oDoc.Tables(1).Cell($iRow+1, $iColumn+1).Range.Text = $aData[$iRow][$iColumn]
    Next
Next

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

:)

My UDFs and Tutorials:

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...