Reputation: 5144
I am using Microsoft.Office.Interop.Word
and this code to duplicate section of Word
document using C#
:
var R1 = FindRange(Document);
R1.Copy();
var R2 = Document.Range(R1.End, R1.End);
R2.Paste();
The range section contains various elements like paragraphs and tables. This code essentially copy/pastes the section of document and works fine.
Is there any other way to achieve same result without using Copy()
and Paste()
functions as they use clipboard (which is big problem)?
Upvotes: 1
Views: 2309
Reputation: 999
I agree that using the clipboard is a bad idea.
Here's an example based on XML that copies the contents of the bookmark named "bookmark" in a source document named "Document2" to the current Selection
range.
void XmlDemo()
{
var wordApp = Globals.ThisAddIn.Application;
var docSource = wordApp.Documents["Document2"];
var rngTarget = wordApp.Selection.Range;
var rngSource = docSource.Bookmarks["bookmark"].Range;
rngTarget.InsertXML(rngSource.XML);
}
I'm unaware of its limitations, but I just tested it to copy some text, a table and a shape without any hassle.
As specified in the comments below, and alternative is to use the FormattedText property of the Range
:
R2.FormattedText = R1.FormattedText;
Upvotes: 2