Good afternoon, tell me, how can I add a code? I need to open a word-template, make edits and add a table at the very end. The first two points were made, but with the addition of a table at the end of the file of the problem.

public static void createWord() { wordapp = new Word.Application(); Object path = @"D:\WorkSpace\Projects\testWord\testWord\testWord\Sh.dot"; try { worddocument = wordapp.Documents.Add(ref path, ref missingObj, ref missingObj, ref missingObj); worddocument.Bookmarks["Well"].Range.Text = "asdfasdf"; //Что нужно писать? } catch (Exception e) { worddocument.Close(ref falseObj,ref missingObj,ref missingObj); wordapp.Quit(ref missingObj,ref missingObj,ref missingObj); worddocument = null; wordapp = null; //throw new Exception(); } wordapp.Visible = true; } 
  • one
    The question does not correspond to the problem - gil9red
  • look here - user2455111
  • Sublihim, dear, can you read? Well, wrote the same as add to the END file. The problem is this, not the addition of the table. - Zurus

2 answers 2

 //Добавляем параграф в конец документа var Paragraph = wordApp.ActiveDocument.Paragraphs.Add(); //Получаем диапазон var tableRange = Paragraph.Range; //Добавляем таблицу 2х2 в указаный диапазон wordApp.ActiveDocument.Tables.Add(tableRange, 2, 2); //"Приукрашиваем" таблицу, иначе по-дефолту она будет без линий var table = wordApp.ActiveDocument.Tables[wordApp.ActiveDocument.Tables.Count]; table.set_Style("Сетка таблицы"); table.ApplyStyleHeadingRows = true; table.ApplyStyleLastRow = false; table.ApplyStyleFirstColumn = true; table.ApplyStyleLastColumn = false; table.ApplyStyleRowBands = true; table.ApplyStyleColumnBands = false; 
     //Создание таблицы: Table tb; /* Если не получится объявить так, то объяви через: public static Table tb; */ tb = doc.Tables.Add(doc.Paragraphs[doc.Paragraphs.Count].Range, n + 4, 6); /* В паметрах указывается: 1.Место,где она начинается; 2.Кол-во строк; 3.Кол-во столбцов */ tb.Borders.Enable = 1;//Делаем границы видимыми