转换为XPS时使用Office Interop问题

时间:2012-01-11 22:04:02

标签: c# wpf office-interop

我正在构建一个简单的应用程序,负责收集在特定forlder中找到的Office文件,然后将它们转换为XPS,以便能够在WPF界面控件中轻松呈现它们。

为此,我正在使用WOrd,Excell,powwerpoint office interopt在我的项目中组装。 到目前为止工作得很好,它转换,但它不时转换所有文件,如魅力和测试后,它只转换一个或两个字段,并返回以下execption:

“由于以下错误,检索具有CLSID {000209FF-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005访问被拒绝。(HRESULT异常:0x80070005(E_ACCESSDENIED))。”

尝试创建Word.Application对象时,异常是retunr

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

这是我的转换方法

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

知道可能出现什么问题吗? 还有一点奇怪的是,根据文档的数量,我可以在创建应用程序对象的同一行接收COMException。我开始真的很生气: - (

问候 哔叽

2 个答案:

答案 0 :(得分:0)

这通常是因为您不是管理员,或者没有管理员权限。一个措辞严密的问题是解决问题的一半,在这种情况下,只需确保您的帐户具有管理员权限,并且正在使用它们。

你是否也可以粘贴你正在使用的代码_wordApplication听起来像参考问题除了使用..你是否通过vs2008或vs2010添加引用确保你复制.Dll's如果这是在你的机器而不是目标机器上运行..那么在机器上运行的不同版本的Office.Interop是一个明确的问题..在项目中为Microsoft.Interop所需的每个.dll ...确保你设置copy local = true;

答案 1 :(得分:0)

好的,还有其他一些问题。在.cs文件顶部的使用中,您是否有以下内容..

    using Microsoft.Office.Interop.Word; 

从哪里

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

将其更改为Application _wordApplication = new Application(); 您传递ref paramMissing的地方 - 将其更改为null将其传递给null,将_wordApplication =设置为null ...将其更改为

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

Com对象的发布方式与管理对象不同