[英]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.