title: Office Interop Does Not Work in Windows Service
body: <p>I have a very weird issue with Microsoft Office. </p>
  
  <p>I have a common library whose sole purpose is to open whatever word document file type is passed to it (by a full file path...) and save that opened word document as a pdf file.</p>
  
  <p>The weird issue is that if I consume that library from a windows service, whenever it attempts to open the word document, I get a null... aka, the word document never got opened. </p>
  
  <p>If however I consume the library from a WPF or Windows Form application I never have any issues. I am aware that there are issues with threading, (Single Thread Appartment) however I have no idea how to fix it to work out of the windows service. :( :( :( </p>
  
  <p>I would appreciate any help! The Error I get Is the following:</p>
  
  <p>Exception Message: {"Object reference not set to an instance of an object."} (Referring to the word document).  Inner Exception: Null; HResult: -2147467261. Data: ListDictionaryInternal with 0 entries; Stack Trace: at DocumentConverter.ToPdf(String currentWorkingFolderPath, String pathToDocumentToConvert) in c:\Project Files...\DocumentConverter.cs:line 209</p>
  
  <p>So here is the library function. It requires the Microsoft Office reference, which is created by the Visual Studio Tools for Office.</p>
  
  <pre><code>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;
  }
  </code></pre>