Reputation: 115
I'm trying to copy text from one Word document to another Word document using bookmarks dynamically through C# code. I'm able to retrieve only the data from the bookmark of one document and insert it into a other document, but the format of the text is changing.
For example, if I add some colour, font in the source document bookmark, the same format is not copied into the target document's bookmark; only the text is copied.
//getting the text from source documents bookmark.
string text = Document1.Bookmarks.get_Item(ref objI).Range.Text.ToString();
//copying the text to Document 2's bookmark
objWordDoc1.Bookmarks.get_Item(booktest).Range.Text = text;
How can I copy the formatting too?
Upvotes: 0
Views: 1861
Reputation: 2865
You have two options:
Use copy and paste, something like
// Copy
Document1.Bookmarks.get_Item(ref objI).Range.Copy();
// Paste
objWordDoc1.Bookmarks.get_Item(booktest).Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
Work with Formatted text property (http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.range.formattedtext(v=office.11).aspx. (I didn't test this)
Range text = Document1.Bookmarks.get_Item(ref objI).Range.FormattedText;
objWordDoc1.Bookmarks.get_Item(booktest).Range = FormattedText;
Upvotes: 1