How do i 开发者_StackOverflowprogramatically 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
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();
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);
Assuming docx, use the DocumentBuilder component of http://powertools.codeplex.com/
For more information, see http://blogs.msdn.com/b/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx
This link should help.
Duplicating Word document using OpenXml and C#
Another suggestion is to creating a copy of the word file and renaming it to whatever the required name is, if that suits your soluition.
http://www.dotnetperls.com/file-copy
If you need to access older Word documents (Word 97 etc), there are third-party libraries available which give you full control over creating and amending the documents without having Word installed on the target machine. This is especially useful for web servers.
We have used Essential DocIO from Syncfusion successfully in the past - http://www.syncfusion.com/products/reporting-edition/docio
Otherwise, you can use the Microsoft Office Automation libraries to access older Word documents - http://support.microsoft.com/kb/301659
There are quite a few things you have to be careful with when using the automation libraries since they execute Word directly on the target machine. Running them on a web server is a big no-no.
精彩评论