Word Interop Copying formatted text in a table cell

I am writing addin for a word to automate editing a protocol template.

There is a table in the template, and I want to extract / copy the generated cell text inside this table.

Now I am doing this:

Range formattedText = agendaTable.Rows[i].Cells[1].Range; string temp = formattedText.WordOpenXML; 

Later I want to paste the text into another cell in the table:

 otherTablesRow.Cells[1].Range.InsertXML(temp); 

The formation is correct, with the exception of the line ("\ r \ a") at the end, which comes from the range in which I extract the text from the cell. The word seems to use a line to mark the end of a cell.

So, after pasting the text into the cell of other tables, I have two lines. How can I avoid duplicate rows? Does anyone know an alternative method for retrieving cell contents?


Update: Perhaps I am asking my question differently.

My common problem is to copy more than one formatted text range into memory and paste it later somewhere in the same document.

+6
source share
2 answers

Try using the following code, it copies text and formatting from one cell to another:

 var copyFrom = agendaTable.Rows[i].Cells[1].Range; var copyTo = otherTablesRow.Cells[1].Range; copyFrom.MoveEnd(WdUnits.wdCharacter, -1); copyTo.FormattedText = copyFrom.FormattedText; 

There is a symbol in the list of pills of the end of the cell that places the target cell in your example; using MoveEnd , we copy everything except the end of the cell character (the last character).

+3
source

You can copy and paste the selection as follows:

 public void Copy() { var selection = (Range)Application.Selection; selection.Copy(); } public void Paste() { var selection = (Range)Application.Selection; selection.PasteSpecial(); } 

If the first choice is the place where you copy, and the second is the place where the copy will be pasted.

EDIT: copying formatted text without using a clipboard (this is a simple copy of XML):

 List<string> copies = new List<string>(); public void Copy() { Microsoft.Office.Interop.Word.Selection wordSelection = Application.Selection; if (wordSelection != null && wordSelection.Range != null) { copies.Add(wordSelection.get_XML()); } } public void Paste(int index) { Microsoft.Office.Interop.Word.Selection wordSelection = Application.Selection; if (wordSelection != null && copies.Count > index) { wordSelection.InsertXML(copies[index]); } } 
+4
source

All Articles