Copying contents of Word doc and paste into another

谁都会走 提交于 2019-11-30 07:41:20

问题


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


回答1:


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();



回答2:


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);



回答3:


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




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/5160964/copying-contents-of-word-doc-and-paste-into-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!