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

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!

Have you founded this feature yet?

Have you found this feature ??

I've tried to find the answer, but I failed.
the best way I've found was what you showed to us.
I put a bookmark at table, and I could handle it.

Thanks for your help.

Hi there,

We could use the Microsoft.Office.Interop.Word.Table.Style property to apply one of the Word built-in styles to a table.

table.Range.Font.Size = 8;
table.set_Style("Table Grid 8");

I don't know how to create custom table style using Interop.Word, but i also found the following toturials which may help to achieve this task.

How to Set Word Table Style in C#, VB.NET

Hope this helps.

What if I do not Know the location where the table is to be inserted. And depending on the number that is known at run time that many times the same table is to be created. So How do I do that? Can you help me with this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.