7

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

In the template is a table and I want to extract/copy the formated text of a cell inside this table.

Right now I am doing it this way:

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

Later I want to paste the text into another tables cell:

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

The formating is correct, except a linebreak ("\r\a") at the end, which comes from the range where I extract the text out of the cell. Seems like word uses the linebreak to mark the cells end.

So after inserting the text into the other tables cell, I have two linebreaks. How can I avoid that duplication of linebreaks? Does someone know an alternative method to retrieve the cells content?


Update: May be I ask my question in another way.

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tobias
  • 2,945
  • 5
  • 41
  • 59
  • does temp contain the \r\a characters before you insert it back into your table? If it does, just use one of the variations of string.Trim, or string.Substring(0,temp.length-2), and truncate the characters – B L Jan 22 '13 at 15:02
  • temp contains the office open xml for the range. and yes, because the "\r\a" is in the text of the range it is included in temp. – Tobias Jan 22 '13 at 15:10
  • So, then can you just truncate them off before you insert the string back in using the InsertXML method, since that presumably is the culprit of adding the additional "\r\a"? If they are the trailing characters in the string, you can chop them off without too much work. Or am I misunderstanding something? – B L Jan 22 '13 at 15:12
  • strangly, if i call Range.Text = Range.Text.Substring(0, Range.Substring.Length - 2) to cut the "\r\a" away, nothing happens. Seems like the setter is "deactivated". – Tobias Jan 22 '13 at 15:16
  • otherTablesRow.Cells[1].Range.InsertXML(temp.Substring(0,temp.Length-2)); Doesn't work? – B L Jan 22 '13 at 15:21
  • No it doesn't, because the office open xml string in temp isn't just the text, but loads of meta xml text. and somewhere in between someone may find the linebreak. – Tobias Jan 22 '13 at 15:23
  • I see. You could still parse out the \r\a in the string if you're sure that it's extraneous, and the source of your problems, using simple string functions. Also, since it looks like you're also only dealing with an individual cell in a loop, could you look for an alternate method of retrieving the cell's contents than defining a Range object? – B L Jan 22 '13 at 15:35
  • This alternative methode to retriev the cells content I was asking for. – Tobias Jan 22 '13 at 15:48

2 Answers2

5

Try the following code, it copies the 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 an end of cell character in the agendaTable Range that messes up the target cell in your example; using MoveEnd we copy everything except the end of cell character (last character).

Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
4

You can copy and paste selection this way:

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

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

Where the first selection is where you are copying from and second is where the copy will be pasted.

EDIT: Copying formatted text without using clipboard (it is simple XML copy):

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]);
    }              
}
Jan Novák
  • 574
  • 3
  • 21
  • Thanks. This works like I had it before. Now the problem is, that I copy content of table cells. When I paste into another cell, another cell (below the cell where I want to paste) is created. – Tobias Jan 25 '13 at 11:02