0
votes

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.

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 :

"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))."

The exception is retunr at the time it try to create the Word.Application object

__wordApplication = new Microsoft.Office.Interop.Word.Application();

Here is my method for converting

 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();
   }

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 :-(

regards serge

2

2 Answers

0
votes

This is often caused because you are not an Administrator, or do not have administrator priveleges. A well phrased problem is half of the cure, and in this case just make sure tht your account has the administrator priveleges, and is using them.

Could you also paste the code where you are using _wordApplication sounds like a reference issue as well besides the using.. did you add the references via vs2008 or vs2010 make sure that you are copying over the .Dll's if this is something that works on your machine and not the Target Machine.. then it's a definite issue with different versions of Office.Interop running on the machine.. in the project for each of the .dlls that are needed for the Microsoft.Interop stuff... make sure that you are setting copy local = true;

0
votes

Ok here are some other questions. In your using at the top of your .cs file, do you have the following..

    using Microsoft.Office.Interop.Word; 

from there where you

__wordApplication = new Microsoft.Office.Interop.Word.Application();

change that to be Application _wordApplication = new Application(); Where you are passing ref paramMissing -- change that to null pass it just like that null where you are setting the _wordApplication = to null... change it to

System.Runtime.InteropServices.Marshal.ReleaseComObject(_wordApplication); 

Com Objects are released differently than managed objects