Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying contents of Word doc and paste into another

How do i programatically copy the contents of one word document and paste it to another word document using C#?

I basically want to copy a personal profile (which is the contents of one word doc) and then insert it into a report.

Any help would be greatly appreciated

Thanks

like image 236
stressed Avatar asked Dec 21 '22 17:12

stressed


2 Answers

You can do this:

object docStart = worddocpromo.Content.End - 1;
object docEnd = worddocpromo.Content.End;

object start = SubDoc.Content.Start;
object end = SubDoc.Content.End;

SubDoc.Range(ref start, ref end).Copy();
Microsoft.Office.Interop.Word.Range rng = worddocpromo.Range(ref docStart, ref docEnd);
rng.Paste();
like image 119
jenny Avatar answered Jan 09 '23 07:01

jenny


You can do this:

    Word.Application word = new Word.Application();
    word.Visible = true;
    Word.Document d1 = word.Documents.Add();
    Word.Document d2 = word.Documents.Open(@"E:\00-Word\Test.docx");
    Word.Range oRange = d2.Content;
    oRange.Copy();
d1.Content.PasteSpecial(DataType:Word.WdPasteOptions.wdKeepSourceFormatting);
like image 29
A.Bahrami Bavani Avatar answered Jan 09 '23 06:01

A.Bahrami Bavani