請分享您的想法!我有問題要檢查的文件夾,並轉換一組具有不同的擴展名的文件的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停止響應,當我嘗試打開此文件手動出現模式窗口文件轉換。在這種情況下該怎麼做
謝謝您的回答我也想過這個辦法解決 我只是想確保沒有其他選擇 – HaikMnatsakanyan 2012-04-05 09:36:02