Add a table using Word Interop and ConvertToTable

The .NET Framework allows us to do many things with relative ease but the main cost is often the time taken in finding a decent example to learn from. I experienced pain when I wanted to create a Word document programmatically and add a simple table to it. But I got there in the end, and this is how I did it.

I’m not going to describe in full how you need to prepare your Visual Studio project. For that, read this. I wanted to utilise the ConvertToTable() method of the Word.Range object which allows us to create a table using a string delimited by tabs and newlines such as this:

Name\tAge\tLocation\nNigel\t44\tUK\nBill\t33\tUSA\nRuben\t86\tNicaragua\n 

The MSDN page Automating Word Tables for Data Insertion and Extraction had taken me so far, but not all the way, so here’s the useful (read: missing) code to bring it all together:

Word.Application app = new Word.Application();
 
Word.Document doc = app.Documents.Add(
    ref oMissing, ref oMissing, ref oMissing, ref oMissing
);
 
Word.Range r = doc.Range(ref oMissing, ref oMissing);
r.Text = "Name\tAge\tLocation\nNigel\t44\tUK\nBill\t33\tUSA\nRuben\t86\tNicaragua\n";
 
object tab = Word.WdTableFieldSeparator.wdSeparateByTabs;
Word.Table t  = r.ConvertToTable(
    ref tab,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing
);

Please leave a comment if you have any questions on this, or if I’ve done something heinous that is a crime against Word automation 😉

Good luck!

 

EDIT: If you need to add more than one item to a blank Word document, you might want to take a look at my subsequent post.

3 thoughts on “Add a table using Word Interop and ConvertToTable

  1. Hi! i have suffered the pains of word tables by myself, as you i had a lot of problems until i found the convertotable solution.

    I would like to know if you have any experience regarding the fact that assigning a lot of text to Range.Text takes some time…

    I have a code that builds lots of tables with this solution but strangely the code that takes time it’s not the convertotable but the assignment of the stringbuilder to that range.

    I’m doing all the basics (undo clear, Realeasing com objects, etc.)

    If you have any idea it would be great!

    Best Regards

  2. David, I can’t help you: my need for Word automation was very simple and once it was done I moved on, so I haven’t had to look into performance. I wonder whether building the table programmatically, adding cells and rows iteratively, might be better… but I realise that writing this code in the first place is a pain and that’s why you’re using ConvertToTable – and there’s no guarantee that this will actually solve your problem.

    Sorry I can’t be of more help. Good luck!

  3. Hi,
    How can i add an image as a column of table data ?
    need to implement a feature like colspan in html. Is it possible ?
    need to add bold text to one column and normal text on other column ?

    Please gives me some links or some sample codes for the same if you know about these?.

    I am working on VSTO C# Word Addin. But VB.Net is OK with me

Leave a Reply