2012-04-04 52 views
1

请分享您的想法!我有问题要检查的文件夹,并转换一组具有不同的扩展名的文件的PDF文件如何以编程方式确定Word应用程序冻结

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

} 

    } 
} 

代码工作,问题是,当我打开一个文档,或任何允许的一些推广各项工作的权利,但我们说有人改变了扩展名的例子DoSomething.exe DoSomething.doc或在文件夹c:\路径是损坏的文档Word停止响应,当我尝试打开此文件手动出现模式窗口文件转换。在这种情况下该怎么做

回答

1

我有类似的情况 - 一个解决方案是创建一个有2个线程的子进程,一个与Word交互,另一个是“看门狗”...“看门狗”线程会重复检查某个模式窗口是否出现以及是否有预先定义的超时时间......在任何这些情况下,它会杀死进程一词,然后等待另一个线程结束 - 如果另一个线程没有在预先定义的时间内结束,则会杀死另一个线程螺纹...

这个工作好,但我在杀字艰难地导致一些如意的副作用,从没有被清理,一些字设置临时文件越来越看不上相同的情况下,观察...

我最终使用第三方库进行此转换,并不需要安装Word ..我对解决方案非常满意,它执行得更好,遇到有问题的文档时,我得到一个我可以处理的异常因此...我使用该库是一个商用......如果这是一个选项,你我可以提供一个链接...

+0

谢谢您的回答我也想过这个办法解决 我只是想确保没有其他选择 – HaikMnatsakanyan 2012-04-05 09:36:02

1

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

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

您可能创作被发射作为转换过程是作为要通过命令行参数接收到Word文档的完整路径一个.NET控制台应用程序的程序。

相关问题