How to copy the content of one word document into another word document?

后端 未结 3 823
一整个雨季
一整个雨季 2021-01-16 16:47

I have a word document with some Text and Images. I want to copy the content of the word document into another word document using C#.

Thanks.

3条回答
  •  春和景丽
    2021-01-16 17:08

    Try the below code . This may help you .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var app = new Microsoft.Office.Interop.Word.Application();
                var sourceDoc =    app.Documents.Open(@"D:\test.docx");
    
                sourceDoc.ActiveWindow.Selection.WholeStory();
                sourceDoc.ActiveWindow.Selection.Copy();
    
                var newDocument = new Microsoft.Office.Interop.Word.Document();
                newDocument.ActiveWindow.Selection.Paste();
                newDocument.SaveAs(@"D:\test1.docx");
    
                sourceDoc.Close(false);
                newDocument.Close();
    
                app.Quit();
    
                Marshal.ReleaseComObject(app);
                Marshal.ReleaseComObject(sourceDoc);
                Marshal.ReleaseComObject(newDocument);
            }
        }
    }
    

提交回复
热议问题