7
votes

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.

2
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 charactersB L
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
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
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
otherTablesRow.Cells[1].Range.InsertXML(temp.Substring(0,temp.Length-2)); Doesn't work?B L

2 Answers

5
votes

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).

4
votes

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]);
    }              
}