I have a very strange problem with Microsoft Office.
I have a shared library whose sole purpose is to open any type of Word document file (full path to the file ...) and save this open document as a PDF file.
The strange problem is that if I use this library from a Windows service, whenever it tries to open a Word document, I get zero ... otherwise, the document document never opens.
If I use the library from a WPF or Windows Form application, I never have any problems. I know that there are problems with thread processing (Single Thread Appartment), however I do not know how to fix this to work from a Windows service. :( :( :(
I would be grateful for any help! The error I get is: the following:
Exception message: {"The reference to the object is not installed in the instance of the object." } (refers to the word document). Internal exception: Null; HResult: -2147467261. Data: ListDictionaryInternal with 0 items; Stack Trace: in DocumentConverter.ToPdf (String currentWorkingFolderPath, String pathToDocumentToConvert) in c: \ Project Files ... \ DocumentConverter.cs: line 209
So here is the library function. This requires a Microsoft Office link created by Visual Studio Tools for Office.
private string ToPDF(string currentWorkingFolderPath, string pathToDocumentToConvert) { string temporaryPdfFolderPath = Path.GetFullPath(currentWorkingFolderPath + "\\pdf\\"); string temporaryPdfFilePath = Path.GetFullPath(temporaryPdfFolderPath + "\\pdffile.pdf"); if (!FileSystem.CreateDirectory(temporaryPdfFolderPath)) { return null; } try { Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application(); object objectMissing = System.Reflection.Missing.Value; wordApplication.Visible = false; wordApplication.ScreenUpdating = false; FileInfo wordFile = new FileInfo(pathToDocumentToConvert); Object fileName = (Object)wordFile.FullName; // This is where it breaks when called from windows service. Use the dummy value as a placeholder for optional arguments Document wordDocument = wordApplication.Documents.Open(ref fileName, ref objectMissing, true, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing); object outputFileName = (object)temporaryPdfFilePath; object fileFormat = WdSaveFormat.wdFormatPDF ; // Save document into PDF Format wordDocument.SaveAs(ref outputFileName, ref fileFormat, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing); // 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)wordDocument).Close(ref saveChanges, ref objectMissing, ref objectMissing); wordDocument = null; // word has to be cast to type _Application so that it will find // the correct Quit method. ((Microsoft.Office.Interop.Word._Application)wordApplication).Quit(ref objectMissing, ref objectMissing, ref objectMissing); wordApplication = null; } catch (Exception ex) { //logging code return null; } return temporaryPdfFilePath; }