繁体   English   中英

如何使用c#将文本从一个Word文档复制到另一个Word文档

[英]How to copy Text from one Word Document to another Word Document using c#

我想将一个Word文档动态复制到另一个Word文档。 此过程可以在“按钮单击”中完成。 文档(docs)包含文本,我想将其复制到文档(docs2)

public void ReadMsWord()
    {
        string filePath = null;
        OpenFileDialog file = new OpenFileDialog();
        file.Title = "Word File";
        file.InitialDirectory = "c:\\";
        file.RestoreDirectory = true;
        if (file.ShowDialog() == DialogResult.OK)
        {
            filePath = file.FileName.ToString();
        }
        try
        {
            //Microsoft.Office.Interop.Word.Application Oword = new Microsoft.Office.Interop.Word.Application();
            //Oword.Visible = true;
            var templatepath = filePath;
            var wordapp = new Microsoft.Office.Interop.Word.Application();
            var orgnldoc = wordapp.Documents.Open(templatepath);
            orgnldoc.ActiveWindow.Selection.WholeStory();
            orgnldoc.ActiveWindow.Selection.Copy();
            var newdcmnt=new Microsoft.Office.Interop.Word.Document();
            newdcmnt.ActiveWindow.Selection.Paste();
            newdcmnt.SaveAs(@"C:\Users\Documents\TestDoc2.docx");
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(orgnldoc);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(newdcmnt);
            GC.Collect();
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }
    }

这是我已经测试过的东西,请确保在进入Marshal.Release code and GC.Collect之前不要停止调试器。发布Marshal.Release code and GC.Collect

我有Office 2010,但在您的“使用”部分中添加以下内容

using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices; 

这就是实现namespace Aliasing

这是下面的代码

var fileName = "TestDoc.docx";
Object oMissing = System.Reflection.Missing.Value;
var oTemplatePath = @"C:\Documents\wrkDocuments\" + fileName;
var wordApp = new Word.Application();
var originalDoc = wordApp.Documents.Open(@oTemplatePath);

originalDoc.ActiveWindow.Selection.WholeStory();
originalDoc.ActiveWindow.Selection.Copy();

var newDocument = new Word.Document();
newDocument.ActiveWindow.Selection.Paste();
newDocument.SaveAs(@"C:\Users\Documents\wrkDocuments\TestDoc2.docx");
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(originalDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(newDocument);
GC.Collect();

更改我为您创建的此方法并传递适当的参数

private static void CopyWordDoc()
{
    var fileName = "TestDoc.docx";
    Object oMissing = System.Reflection.Missing.Value;
    var oTemplatePath = @"C:\Documents\wrkDocuments\" + fileName;
    var wordApp = new Word.Application();
    var originalDoc = wordApp.Documents.Open(@oTemplatePath);
    // you can do the line above by passing ReadOnly=False like this as well
    //var originalDoc = wordApp.Documents.Open(@oTemplatePath, oMissing, false);
    originalDoc.ActiveWindow.Selection.WholeStory();
    originalDoc.ActiveWindow.Selection.Copy();

    var newDocument = new Word.Document();
    newDocument.ActiveWindow.Selection.Paste();
    newDocument.SaveAs(@"C:\Documents\wrkDocuments\TestDoc2.docx");
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(originalDoc);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(newDocument);
    GC.Collect();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM