我正在使用 C# 从另一个 Word 文档(例如:base.doc)创建一个 Word 文档(例如:child.doc)。
base.doc 中有一个图像放在标题中。我正在从 base.doc 复制内容并使用 C# 将内容粘贴到 child.doc 中。
在此过程之后,我将通过代码保存 child.doc。如果我手动(而不是通过代码)打开保存的文档(即:child.doc),则标题部分中的图像丢失。我究竟做错了什么?
我正在使用以下代码:
string path = "base.doc"; // path of base document
object format = (object)Word.WdSaveFormat.wdFormatDocument;
object formatPdf = (object)Word.WdSaveFormat.wdFormatPDF;
object readOnly = true;
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
Word._Application wordApp = new Microsoft.Office.Interop.Word.Application();
object file = @path;
object nullobj = System.Reflection.Missing.Value;
wordApp.Visible = false;
wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
Word._Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref readOnly,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
doc.Activate();
Word.Range docRange = doc.Content;
// After these codes i do some replacement in the conten like as follows
docRange.Find.Text = "Some text to replace";
docRange.Find.Replacement.Text = "Replacement text";
docRange.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
docRange.Find.Forward = true;
object rangeStart = docRange.Start;
object rangeEnd = docRange.End;
Word.Range rng = doc.Range(ref rangeStart, ref rangeEnd);
rng.Font.Name = "Arial";
Word._Document inewDoc = wordApp.Documents.Add(ref nullobj, ref nullobj, ref nullobj, ref nullobj);
rng.Copy();
inewDoc.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
inewDoc.SaveAs(ref tempPath, ref format, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj,
true, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
doc.Close(ref saveChanges, ref nullobj, ref nullobj);
doc = null;
inewDoc.Close(ref saveChanges, ref nullobj, ref nullobj);
inewDoc = null;
wordApp.Quit(ref saveChanges, ref nullobj, ref nullobj);
wordApp = null;