title: Using Office Interopt trouble while converting to XPS
body: <p>I am building a simple application which is in charge of collecting Office files found in a specific forlder and then convert them to XPS in order to be able to present them easily in a WPF interface control.</p>
  
  <p>For that I am using WOrd,Excell, powwerpoint office interopt assemblied in my project.
  Works great so far, it converts but time to time it converts all documents like a charm and test after it converts only one or 2 field and there return the following execption :</p>
  
  <p>"Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))."</p>
  
  <p>The exception is retunr at the time it try to create the Word.Application object</p>
  
  <pre><code>__wordApplication = new Microsoft.Office.Interop.Word.Application();
  </code></pre>
  
  <p>Here is my method for converting</p>
  
  <pre><code> private static void ConvertFromWord(IDocument Doc)
      {
  
          __wordApplication = new Microsoft.Office.Interop.Word.Application();
          __wordApplication.Visible = false;
          __wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
  
  
          //if (__isWordInitialized == false)
          //    InitializeWord();
  
          Word.Document wordDocument = null;
  
          object pSourceDocPath = Path.Combine(new Uri(Doc.OriginalPath).LocalPath,Doc.OriginalFile);
          object paramMissing = Type.Missing;
  
  
          string pExportFilePath =MyExtensions.IsNullOrWhiteSpace(Doc.ConvertedFile) ? GetUniqueXpsFile(new Uri(Doc.ConvertedPath).LocalPath) : Doc.ConvertedFile;
  
          Doc.ConvertedFile = Path.GetFileName(pExportFilePath);
  
          try
          {
              var pExportFormat = Word.WdExportFormat.wdExportFormatXPS;
              bool pOpenAfterExport = false;
              var pExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen;
              var pExportRange = Word.WdExportRange.wdExportAllDocument;
              int pStartPage = 0;
              int pEndPage = 0;
              var pExportItem = Word.WdExportItem.wdExportDocumentContent;
              var pIncludeDocProps = true;
              var pKeepIRM = true;
              var pCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
              var pDocStructureTags = true;
              var pBitmapMissingFonts = true;
              var pUseISO19005_1 = false;
              //var pFixedFormatExtClassPtr=;
  
              try
              {
                  try
                  {
                      wordDocument = __wordApplication.Documents.Open(ref pSourceDocPath,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing,
                                                                    ref paramMissing
                          );
                  }
                  catch (Exception exc)
                  {
                      //return new OfficeToXpsConversionResult(Solatys.Office.Lib.Types.Types.ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc);
                  }
  
                  if (wordDocument != null)
                  {
                      try
                      {
                          wordDocument.ExportAsFixedFormat( 
                                              pExportFilePath,
                                              pExportFormat,
                                              pOpenAfterExport,
                                              pExportOptimizeFor,
                                              pExportRange,
                                              pStartPage,
                                              pEndPage,
                                              pExportItem,
                                              pIncludeDocProps,
                                              pKeepIRM,
                                              pCreateBookmarks,
                                              pDocStructureTags,
                                              pBitmapMissingFonts,
                                              pUseISO19005_1,
                                              ref paramMissing
                                          );
                      }
                      catch (Exception exc)
                      {
                         // return new OfficeToXpsConversionResult(Solatys.Office.Lib.Types.Types.ConversionResult.ErrorUnableToExportToXps, "Word", exc);
                      }
  
  
                  }
                  else
                  {
                      //return new OfficeToXpsConversionResult(Solatys.Office.Lib.Types.Types.ConversionResult.ErrorUnableToOpenOfficeFile);
                  }
              }
              finally
              {
                  // Close and release the Document object.
                  if (wordDocument != null)
                  {
                      wordDocument.Close(ref paramMissing, ref paramMissing,ref paramMissing);
                      wordDocument = null;
                  }
  
                  // Quit Word and release the ApplicationClass object.
                  if (__wordApplication != null)
                  {
                      __wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                      __wordApplication = null;
                  }
  
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                  __isWordInitialized=false;
              }
          }
          catch (Exception exc)
          {
              //return new OfficeToXpsConversionResult(Solatys.Office.Lib.Types.Types.ConversionResult.ErrorUnableToAccessOfficeInterop, "Word", exc);
          }
  
          Doc.ConvertedFile = pExportFilePath;
          KillWord();
     }
  </code></pre>
  
  <p>Any idea what could be wrong ?
  What is strange also is that depending on the amount of document, I can receive a COMException at the same line where it create the application object. I start to be really mad :-(</p>
  
  <p>regards
  serge</p>