C# Word Automation creating Tables
Hi,
Im trying to include the feature of automatically generating a word document through C# using a pre-defined template. Im able to insert 'mailmerge' fields in the word template documents at specific locations, and accessing these through the code in C#. I can update the values of these fields, no problem.
I am now trying to insert a table at a specific location. I would like to dynamically set the number of rows and columns, back color, fore color of the cell, font size etc. and even formulas with cell referencing (like you have in excel).
Is it possible to do this using C#? i looked at a few posts online, but did'nt really find a solution. some were not working.
Thanks
jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
Skill Endorsements: 0
Well i've figured out how to create tables.. It goes something like this if anyone is facing the same problem:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
app.Visible = false;
object start = 0;
object end = 0;
object oNull = System.Reflection.Missing.Value;
Document doc = new DocumentClass();
doc = app.Documents.Add(ref oNull, ref oNull, ref oNull, ref oNull);
Microsoft.Office.Interop.Word.Table tbl = doc.Tables.Add(doc.Range(ref start, ref end), 10, 2, ref oNull, ref oNull);
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
tbl.Rows[i + 1].Cells[1].Range.Text = "Run# :" + ((int)i + 1).ToString();
tbl.Rows[i + 1].Cells[2].Range.Text = "Value :" + rnd.Next(0, 2000).ToString();
}
However my next problem is formatting the cells for this table that's just created. Until now i am only able to find how to set the width and height of cells/rows/columns for the table. I need to find out how to change the backcolor/forecolor, font style etc for a particular cell or the entire row.
Any help will be appreciated!
jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
Skill Endorsements: 0