So, I have this Word Document template with an existing table in it. I am having some difficulties in finding references on how to insert new rows on the existing table.
First thing I want to know is, how will I identify if the table inside my word document template is the existing table?
Secondly, how will I populate the table with data?
I tried this link as a reference https://msdn.microsoft.com/en-us/library/vstudio/w1702h4a.aspx but don't know how to embed it to my program.
I also tried creating new table as an alternative solution with the following code:
object m = System.Reflection.Missing.Value;
object oldFileName = (object)"E:\\Fake Bill.docx";
object readOnly = (object)false;
Word.Application ac = null;
ac = new Word.Application();
// Now we open the document.
Word.Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
ref m, ref m, ref m, ref m, ref m, ref m, ref m,
ref m, ref m, ref m, ref m, ref m, ref m);
object start = 0;
object end = 0;
Word.Range myRange = doc.Range(ref start, ref end);
Word.Table myTable = doc.Tables.Add(myRange, 2, 3);
int rowCount = 2;
List<string> collectionOfStrings = new List<string>();
collectionOfStrings.Add("hello");
collectionOfStrings.Add("hi");
//add a row for each item in a collection.
foreach( string s in collectionOfStrings)
{
myTable.Rows.Add(ref m);
// do somethign to the row here. add strings etc.
myTable.Rows[rowCount].Cells[1].Range.Text = "Content of column 1";
myTable.Rows[rowCount].Cells[2].Range.Text = "Content of column 2";
myTable.Rows[rowCount].Cells[3].Range.Text = "Content of column 3";
//etc
rowCount++;
}
This code works fine. My only problem now is to identify an existing table.
This answer is way late, but in case you never figured it out:
You can set the alt text for the table within Word (right click > Table Properties > Alt Text), then use that to single out the desired table.