请分享你的想法!我在检查文件夹并转换 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:\ 路径中的示例 DoSomething.exe DoSomething.doc 的扩展名文档已损坏Word 停止响应,当我尝试手动打开此文件时,会出现一个模式窗口文件转换。在这种情况下该怎么办