Chau Chau - 3 months ago 33
C# Question

Microsoft Word 2007 VSTO, Create table outside word?

I am using VSTO to fill data into a table in a Microsoft Word 2007 template. The amount of data varies and filling many pages (+50) takes a lot of time.

The code I use to create a table:

Word.Table table = doc.Tables.Add(tablePosition,
ref System.Reflection.Missing.Value,
ref System.Reflection.Missing.Value);

I suspect that the time consumption is due to the communication between Visual Studio (C#) and Word each time I insert data into a cell. If this is the case, it might be faster to create the table in C# and afterwards insert it into Word.

The Microsot.Office.Interop.Word.Table is an abstract class - thus I cannot do this

Word.Table table = new Word.Table();

which would have been handy.

Are there other possibilities when just using VSTO?

  • Try creating the table in HTML Clipboard format, add to clipboard, then paste.
  • Try creating the table in HTML and inserting it.
  • Try creating tab-delimited string with newline character for each record. Insert string with selection, convert selection to table using tabs as delimiter.
  • Create template as XML, transforming data with Xslt into Word XML Document.
  • Create template as a "Directory Mail Merge", perform mail merge with data.

Depending on your requirements, I recommend using the mail merge technique because the user can edit the template and mail merges are fast, especially if you have 50+ pages.