【问题标题】:how to programmatically determine the Word application freeze如何以编程方式确定 Word 应用程序冻结
【发布时间】:2012-04-18 12:00:30
【问题描述】:

请分享您的想法!我无法检查文件夹并在 PDF 中转换一组具有不同扩展名的文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object oMissing = System.Reflection.Missing.Value;
            word.Visible = false;
            word.ScreenUpdating = false;


            object aa = WdOpenFormat.wdOpenFormatAuto;
            string errorMessage = null;


            word.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            //selection extension 

            var allExtentionGroupFiles = Directory.GetFiles(@"C:\path", "*.*").
                Where(s=>!s.Contains("~$") && (s.EndsWith(".docx") 
                || s.EndsWith(".doc")
                || s.EndsWith(".docm")
                || s.EndsWith(".dotx")
                || s.EndsWith(".dotm")
                || s.EndsWith(".dot")
                || s.EndsWith(".mht")
                || s.EndsWith(".mhtml")
                || s.EndsWith(".rtf")
                || s.EndsWith(".txt")
                || s.EndsWith(".xml")
                || s.EndsWith(".odt")
                || s.EndsWith(".wps"))).
                GroupBy(s=>s.Substring(s.LastIndexOf('.'))).OrderBy(s=>s.Key);

            foreach (var currentExtentionGroup in allExtentionGroupFiles)
            {

                Console.WriteLine("-->>{0}", currentExtentionGroup.Key);
                foreach (var currentDoc in currentExtentionGroup)
                {

                    Object filename = (Object)currentDoc;



                    try
                    {
                        //open current document

                        Document document = word.Documents.Open(filename,ConfirmConversions:aa,OpenAndRepair:true,Revert:true);

                        document.Activate();


                        object outputFileName = currentDoc.Replace(currentExtentionGroup.Key, ".pdf").Insert(10, "TEST");
                        object fileFormat = WdSaveFormat.wdFormatPDF;


                        document.SaveAs(ref outputFileName,
                        ref fileFormat, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        document.Close();

                    }
                    catch (Exception e1)
                    {
                        errorMessage = e1.ToString();

                    }
                }
            }

word.Quit();

}

    }
}

代码正在运行,问题是当我打开一个文档时,或者任何允许的扩展都可以正常工作,但是假设有人更改了文件夹 c:\path 上或中的示例 DoSomething.exe DoSomething.doc 的扩展名文档损坏,Word 停止响应,当我尝试手动打开此文件时,会出现一个模式窗口文件转换。这种情况下怎么办

【问题讨论】:

    标签: c# ms-word pdf-generation office-interop docx


    【解决方案1】:

    我也遇到过类似的情况 - 一种解决方案是创建一个具有 2 个线程的子进程,一个与 Word 交互,另一个是“看门狗”……“看门狗”线程会反复检查是否出现了某个模态窗口和预定义的超时是否... ...

    这工作正常,但我观察到在相同的情况下,以硬方式杀死单词会导致一些令人不快的副作用,从临时文件未清理到某些单词设置被丢弃......

    我最终使用 3rd-party 库进行此转换,根本不需要安装 Word。我对解决方案非常满意,它的性能要好得多,如果文档有问题,我会遇到异常,我可以相应地处理...我使用的图书馆是商业图书馆...如果您可以选择,我可以提供链接...

    【讨论】:

    • 感谢您的回答我也想过这种方式解决我只是想确保没有其他选择
    【解决方案2】:

    不幸的是,据我所知,Office 对象模型不提供任何检测或恢复 Office 应用程序冻结的方法。您甚至不需要破坏文档;对于其他有效的文档,Word 到 PDF 的转换有时会冻结。

    我找到的唯一解决方案是生成另一个进程(不仅仅是线程),它在单个文档上执行转换,并让您的主进程等待它在有限的时间段内完成(比如 5 分钟) .如果超时,那么您的主进程应该终止转换进程并将文档标记为不可处理。

    您可以将要作为转换过程启动的程序编写为 .NET 控制台应用程序,该应用程序通过命令行参数接收 Word 文档的完整路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多