gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:00 31 4
gpt4 key购买 nike

我想动态复制一个word文档到另一个word文档。这个过程可以在 Button Click 中完成。文档(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 代码和 GC.Collect

之前不要停止调试器

我有 office 2010,但在您的使用部分添加以下内容

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

这就是您如何实现命名空间别名

下面是代码

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

关于c# - 如何使用c#将文本从一个Word文档复制到另一个Word文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524194/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com