C # 2010 Word - creating tables without an empty row between

With C # 2010, I need to open a Word 2010 template, find a bookmark and insert a table there. In fact, it should be a “three-part table”: one row with two columns, then several rows with five columns and three columns as separate columns. And it should look like one table with no paragraphs or blank lines between them.

My experience with word automation is quite limited. I can find examples of how to create a table in a bookmark - no problem so far, but how to add a new table right after it ...

Thanks for the help!

0
c # ms-word
source share
1 answer

That should do the trick. You do not need multiple tables to have different columns; Word allows you to have one table in which the first row has 2 columns, the next 3 rows have 5 columns, and the last 3 rows contain only 1 column. (You did not say how many rows you need with five columns, so I just went with 3.)

//Be sure to add this reference: //Project>Add Reference>.NET tab>Microsoft.Office.Interop.Word //open Word App Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application(); //make it visible or it'll stay running in the background msWord.Visible = true; //open a new document based on the Word template. //You shouldn't open the template directly using msWord.Documents.Open(path) unless you want to edit the template itself. Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Add(@"c:\MyTemplate.dotx"); //find the bookmark string bookmarkName = "BookmarkToFind"; if (wordDoc.Bookmarks.Exists(bookmarkName)) { Microsoft.Office.Interop.Word.Bookmark bk = wordDoc.Bookmarks[bookmarkName]; //set the document range to immediately after the bookmark. //If you want to add the table *into* the bookmark, it needs to be done differently. //This page has a good explanation of the differences between adding to the bookmark range vs adding after the bookmark range. //http://gregmaxey.mvps.org/word_tip_pages/insert_text_at_or_in_bookmark.html //It a little more hassle because you have to re-add the bookmark after inserting into it, //so inserting after the bookmark is usually fine less you're going to be inserting text programmatically at the same bookmark a second time. Microsoft.Office.Interop.Word.Range rng = wordDoc.Range(bk.Range.End, bk.Range.End); //create a table with 8 rows and 5 columns into the range. Microsoft.Office.Interop.Word.Table tbl = wordDoc.Tables.Add(rng, 8, 5); //set the table borders. tbl.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; tbl.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; //merge the cells in the first row down to 2 columns (Word cells start at 1, not at 0). tbl.Cell(1, 1).Merge(tbl.Cell(1, 3)); //distribute the columns evenly tbl.Rows[1].Select(); msWord.Selection.Cells.DistributeWidth(); //rows 2-5 already have 5 columns so don't touch them. //merge rows 6-8 into single-columns rows. for (int x = 6; x < 9; x++) { tbl.Cell(x,1).Merge(tbl.Cell(x,5)); } //put the cursor in the table first cell. rng=wordDoc.Range(tbl.Cell(1,1).Range.Start, tbl.Cell(1,1).Range.Start); rng.Select(); 
+1
source share

All Articles