A code sample for reference:
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordapp1 = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document aDoc1 = null;
if (File.Exists((string)filepath))
{
DateTime toDay = DateTime.Now;
object readOnly = false;
object isVisible = true;
//set word not visible;
wordapp1.Visible = false;
//open word File
aDoc1 = wordapp1.Documents.Open(ref filepath, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
aDoc1.Activate();
//find the Word and Replace them with values
this.FindAndReplace(wordapp1, "@Name", Name);
}
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
aDoc1.SaveAs(ref SaveAs, ref format, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
//aDoc1.Close(ref saveOption, ref originalFormat, ref routeDocument);
//wordapp1.Quit(ref missing, ref missing, ref missing);
((Microsoft.Office.Interop.Word._Document)aDoc1).Close(ref saveOption, ref originalFormat, ref routeDocument);
//aDoc1.Close(ref saveOption, ref originalFormat, ref routeDocument);
((_Application)wordapp1).Quit(ref missing, ref missing, ref missing);
No comments:
Post a Comment