开发者

C# Word Interop, Pasting Clipboard into Paragraph

开发者 https://www.devze.com 2023-04-06 12:19 出处:网络
I have a report builder I\'m making that builds the start of a report in word. Word is then kept open for the user to carry on with the report. The report builder gets rtf from a database that is to b

I have a report builder I'm making that builds the start of a report in word. Word is then kept open for the user to carry on with the report. The report builder gets rtf from a database that is to be used as paragraphs. For each rtf a paragraph is created within the document and the rtf added to it.

What I've read seems to imply that if I want to insert rtf into a word document then I place it in the clipboard then paste it from the clipboard into word. This works fine except th开发者_如何学编程is doesn't actually put it into the paragraph how I want it. When the paste method is called it doesn't actually place the rtf in the paragraph range, it just pastes it where the paragraph starts. It doesn't overwrite the paragraph, the paragraph still exists below the block of rtf I pasted is. Moving the range by one to accomodate the paragraph marks doesn't work it just pastes it below the paragraph instead. What I think it needs to use is an Insert() method somehow rather than a Paste() method however I don't know how to go about this and I can't find any information how to go about it. I can insert from building blocks or inserting plain text fine, but what is being inserted here needs to be formatted text. The formatted text has a mix of styles etc that a different user makes.

The code for copying to the clipboard and pasting is as follows:

Clipboard.SetText(richTextBox1.Rtf, TextDataFormat.Rtf);
oPara[i].Range.Paste();

I know you can insert building blocks into paragraphs, which keeps the paragraph formatting, with the following:

tTemplate.BuildingBlockEntries.Item(foundList[i]).Insert(oPara[i].Range.FormattedText);

but I can't find how you would achieve this in my scenario.

The reason I'd like to insert it into a paragraph is to edit certain aspects of the format and ensure things such as don't break lines across pages etc.

The code I have currently for the creating and inserting is this:

Word.Paragraph[] oPara = new Word.Paragraph[foundList.Count];

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    Clipboard.SetText(foundList[i].Paragraph, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}

I checked to see where a paragraph was exactly visually with the line

oPara[0].Range.Select(); //To see first paragraph

The result has the rtf pasted where the paragraph begins and the paragraph is just below the rtf pasted. How would you go about inserting rtf the way I want to into a paragraph in MS-Word?

EDIT: calling the collapse method doesn't do what I want to happen


Found the solution, the paste method does actually paste inside the paragraph tags, the problem is that when text is entered into a rich text box, the rich text box automatically adds paragraph tags whether there was any to begin with or not. As the rtf had paragraph tags it counted it as a different paragraph. So the rtf from the database had paragraph tags that were added by a rich text box and not the user. To fix this I just removed the paragraph tags from the rtf taken from the database. Heres the code I ended up with:

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    string tempS = foundList[i].Paragraph;
    tempS = tempS.Replace("\\pard", "");
    tempS = tempS.Replace("\\par", "");
    Clipboard.SetText(tempS, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号