C# word文档克隆现有的word文件作为新文档
C# word document clone existing word file as a new document
我想创建应用程序来填充由 C# Interop.Word 库自动填充的模板文档。用户从现有文件中选择模板文档。我必须将此文档克隆到新文档(包括填充、边距和所有格式)。
1.I 已尝试使用 InsertFile 函数
document = app.Documents.Add();
object missing = System.Reflection.Missing.Value;
object start = 0;
object end = 0;
Word.Range range = document.Range(ref start, ref end);
range.InsertFile(template_file_name, ref missing, ref _true, ref missing, ref missing);
并在文档中填充模板。但是这个函数改变了模板中的一些格式
2. 我试过使用 Copy 和 PasteSpecial
app=new Word.Application();
src_doc=app.Documents.Open(template_file_name);
document=app.Documents.Add();
src_doc.Content.Copy();
document.Content.PasteSpecial(DataType:Word.WdPasteOptions.wdKeepSourceFormatting);
此代码保持格式正确。但是粘贴后改变值是有问题的。 "src_doc" 关闭后仍保持打开状态。
Long story short. Need to clone existing document as a new Document
这可以通过将文件路径传递给 Docments.Add 方法来实现:
Word.Application _word = new Word.Application();
_word.Visible = true;
_word.WindowState = Word.WdWindowState.wdWindowStateMaximize;
Word.Document _doc = _word.Documents.Add(pathToExistingDocument);
我想创建应用程序来填充由 C# Interop.Word 库自动填充的模板文档。用户从现有文件中选择模板文档。我必须将此文档克隆到新文档(包括填充、边距和所有格式)。
1.I 已尝试使用 InsertFile 函数
document = app.Documents.Add();
object missing = System.Reflection.Missing.Value;
object start = 0;
object end = 0;
Word.Range range = document.Range(ref start, ref end);
range.InsertFile(template_file_name, ref missing, ref _true, ref missing, ref missing);
并在文档中填充模板。但是这个函数改变了模板中的一些格式
2. 我试过使用 Copy 和 PasteSpecial
app=new Word.Application();
src_doc=app.Documents.Open(template_file_name);
document=app.Documents.Add();
src_doc.Content.Copy();
document.Content.PasteSpecial(DataType:Word.WdPasteOptions.wdKeepSourceFormatting);
此代码保持格式正确。但是粘贴后改变值是有问题的。 "src_doc" 关闭后仍保持打开状态。
Long story short. Need to clone existing document as a new Document
这可以通过将文件路径传递给 Docments.Add 方法来实现:
Word.Application _word = new Word.Application();
_word.Visible = true;
_word.WindowState = Word.WdWindowState.wdWindowStateMaximize;
Word.Document _doc = _word.Documents.Add(pathToExistingDocument);