2013-03-20 77 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

相關問題