2013-03-20 78 views
2

我想将一个word文档动态复制到另一个word文档。这个过程可以在按钮点击中完成。文档(文档)包含的文字,我想将它复制到文件(docs2)如何使用c将文本从一个Word文档复制到另一个Word文档#

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()); } 
    } 
+0

通过你的问题是我们都有自己的需求和欲望,但我看不出有问题的代码这里是 – MethodMan 2013-03-20 13:01:20

+0

什么是错误?你尝试了什么? – evgenyl 2013-03-20 13:01:44

+0

方法'PasteSpecial'没有超载需要1个参数 – user2173324 2013-03-20 13:06:02

回答

0

这里是什么,我已经测试确保你不会停止调试程序,直到你得到的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(); 

改变这个方法,我已经为您创建,并在适当的PARAMS

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(); 
} 
+0

它不工作它给出错误没有超载的方法'另存'和'打开'和'采取'1'参数 – user2173324 2013-03-20 16:41:51

+0

你使用什么版本的Word ..?还有什么大会你添加到您的参考..?我引用'版本14.0'你能告诉我你的文件的使用部分以及你正在使用的更新的代码..这个作品我只需要知道你使用的版本,以便我可以添加组件并测试它.. – MethodMan 2013-03-20 16:43:06

+0

microsoftoffice 2007 – user2173324 2013-03-20 16:46:10

相关问题