Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

How to always reference the last table in a Word document using Word Interop

$
0
0

I am writing data to dynamic tables using Word Interop. There can be 0 to many rows in each table.

When a table is to be written, one of the earlier tables is copied, the rows removed, and new rows added as necessary for each line of data.

Unfortunately my code is throwing an exception that it has lost a refrence to the Word Document tables after a new page is added.

This is happening on a specific table that has been put on a second page, and the error happens on this line of code:   

oWordDoc.Tables[iTable].Cell(oWordDoc.Tables[iTable].Rows.Count, oWordDoc.Tables[iTable].Columns.Count).Range.ParagraphFormat.KeepTogether = -1;

What's happening is that the there is a new tables collection started for the second page so my code is trying to reference a table that does not exist in the current tables collection.

How can I always reference the last table of the document or fix my code?


Viewing all articles
Browse latest Browse all 4350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>