簡體   English   中英

C#Word文檔將現有Word文件克隆為新文檔

[英]C# word document clone existing word file as a new document

我想創建應用程序來填充由C#Interop.Word庫自動執行的模板文檔。 用戶從現有文件中選擇模板文檔。 我必須將此文檔克隆到新文檔(包括填充,邊距和所有格式)。

1.我試圖使用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”仍保持打開狀態。

長話短說。 需要將現有文檔克隆為新文檔

這可以通過將文件路徑傳遞到Docments.Add方法來實現:

Word.Application _word = new Word.Application();
_word.Visible = true;
_word.WindowState = Word.WdWindowState.wdWindowStateMaximize;

Word.Document _doc = _word.Documents.Add(pathToExistingDocument);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM