S
Unify cells using https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.cell.merge?view=word-pia ://первая колонка
tbl.Cell(i + 2 - y, 1).Merge(tbl.Cell(i + 2, 1));
//вторая колонка
tbl.Cell(i + 2 - y, 2).Merge(tbl.Cell(i + 2, 2));
You're trying to set a range not in the table, but in Word and merge all cells in it. The error arises when the method is challenged Merge♪ Apparently the range doesn't work well. A little bit of an example.The error occurs when the cells are combined. The first step in the example is therefore to separate the operation from the rest of the logic. To this end, we will create a document containing a table with one cell and try to combine:var word = new Application();
var doc = word.Documents.Open(@"C:\temp\111.docx");
word.Visible = true;
var tbl = word.ActiveDocument.Tables[1];
tbl.Rows.Add();//Добавляем в таблицу строку.
doc.Range(tbl.Cell(1, 1).Range.Start, tbl.Cell(2, 1).Range.End).Cells.Merge();
This code will lead to an error and we have already received an easily reproduced example that can be launched in a console. But it can be improved. To this end, it may be possible to specify what particular characteristic/method causes an exception. This can be done by reading the transcript by mistake:StackTrace:
at Microsoft.Office.Interop.Word.Cells.Merge()
at WordMergeCells.Program.Main(String[] args)
It's obvious that error occurs in the method. Merge♪ Let's put this in an example:var cells = doc.Range(tbl.Cell(1, 1).Range.Start, tbl.Cell(2, 1).Range.End).Cells;
cells.Merge(); //здесь ошибка
It'll be a good example, but if you want it, you can improve it. For example, it refers to external document Word. It's easy to create, but it's even easier if it's in the code:var word = new Application { Visible = true };
var doc = word.Documents.Add();
var tbl = doc.Tables.Add(doc.Range(), 2, 1);
tbl.Borders.Enable = 1;
var cells = doc.Range(tbl.Cell(1, 1).Range.Start, tbl.Cell(2, 1).Range.End).Cells;
cells.Merge(); //здесь ошибка
With this example, you'll get the answer much faster.