首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在转换为XPS时使用Office Interopt问题

在转换为XPS时使用Office Interopt问题
EN

Stack Overflow用户
提问于 2012-01-12 06:04:02
回答 2查看 1.5K关注 0票数 0

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

为此,我在我的项目中使用了WOrd、Excell、powwerpoint office interopt。到目前为止,它的工作很好,它转换,但有时它转换所有的文档像一个符串和测试后,它只转换一个或2个字段,并在那里返回以下执行:

“检索CLSID为{000209FF-0000-0000-C000-000000000046}的组件的COM类工厂失败,原因是出现以下错误: 80070005访问被拒绝。(来自HRESULT的异常: 0x80070005 (E_ACCESSDENIED))。”

在尝试创建Word.Application对象时,将返回异常

代码语言:javascript
复制
__wordApplication = new Microsoft.Office.Interop.Word.Application();

下面是我的转换方法

代码语言:javascript
复制
 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。我开始非常生气了:-(

向serge致敬

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-12 22:38:00

好的,这里有一些其他的问题。在使用.cs文件的顶部时,是否包含以下内容..

代码语言:javascript
复制
    using Microsoft.Office.Interop.Word; 

从那里你可以

代码语言:javascript
复制
__wordApplication = new Microsoft.Office.Interop.Word.Application();

在传递ref paramMissing的地方将其更改为Application _wordApplication = new Application(); --将其更改为null,就像您将_wordApplication =设置为null一样传递它...将其更改为

代码语言:javascript
复制
System.Runtime.InteropServices.Marshal.ReleaseComObject(_wordApplication); 

Com对象的释放方式与托管对象不同

票数 0
EN

Stack Overflow用户

发布于 2012-01-12 06:22:41

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

除了使用..之外,您还可以将代码粘贴到您使用_wordApplication的地方吗?听起来像是一个参考问题。您是否通过vs2008或vs2010添加引用?如果.Dll可以在您的计算机上运行,而不是在目标计算机上运行,请确保您正在复制.Dll。那么在机器上运行不同版本的Office.Interop肯定是个问题。在项目中,对于Microsoft.Interop所需的每个.dlls ...确保您设置的是copy local = true;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8827356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档