I have used to below code to convert RTF files to PDF. This code is compiled into an exe file and embedded in SSIS 2012. It works stable for few months. However, SSIS failed recently when it tried to convert a file "12345.rtf" into PDF. The temp file "~$12345.pdf" is made but it cannot save into PDF. If i deleted this temp file manually then SSIS(or exe file eventually) could save RTF file as PDF successfully, but then the temp file is generated again at this location, and SSIS failed with this message "The process time out". If I copied the RTF file to other location and the code (exe file) can save it to pdf successfully and the temp file is gone after PDF file is made. I cannot restart the production server to see if the problem will come again. My temporary solution is to hard code to exclude this RTF file from further processing. So is there any way to restart the Interop service so the temp file will not be generated again? or is there any solution to prevent it happens?
try
{
if (System.DateTime.Today <= DateTime.Parse("2021-01-01"))
{
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"D:\Files");
FileInfo[] wordFiles = dirInfo.GetFiles("*.rtf");
word.Visible = false;
word.ScreenUpdating = false;
foreach (FileInfo wordFile in wordFiles)
{
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
// Use the dummy value as a placeholder for optional arguments
Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
// declare range for word document
Microsoft.Office.Interop.Word.Range rng = doc.Content;
rng.Fields.Update();
object outputFileName = wordFile.FullName.Replace(".rtf", ".pdf");
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
}
catch (Exception ex)
{
throw;
}