我正在使用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;
答案 0 :(得分:0)
我找到了解决方案。实际上问题出在我的图像格式上。我使用.tif格式它不使用word appliction.So我改变.tif格式为.png格式。现在它工作正常。 -