【发布时间】:2019-08-02 05:23:46
【问题描述】:
尝试设置使用 Word 作为中介将 pdf 转换为 html 的 c# 控制台程序。登录时从命令行工作,但从调度程序运行时不会这样做。 现在我知道 MS 不正式支持这个-https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office - 但我仍然希望追求这条道路。 我看过各种有关解决方法的帖子,但未能使其正常工作。
简而言之,程序在打开文档时停止。
谁能建议可以采取哪些步骤来解决这个 MS 实施的限制。
namespace InterOpTest
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
String sFolder = "C:\\temp";
String sInputFile =sFolder+"\\input.pdf";
String sOutputFile = sFolder+"\\test\\output.html";
String sLogFile = sFolder + "\\Interoptest.log";
System.IO.DirectoryInfo d = new DirectoryInfo(sFolder);
// StreamWriter sw= new StreamWriter(sLogFile);
StreamWriter sw = File.AppendText(sLogFile);
try
{
sw.WriteLine("");
sw.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + ", Deleting any pre-existing version of " + sOutputFile);
File.Delete(sOutputFile);
sw.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + ", Opening "+sInputFile);
oWord.Documents.Open(sInputFile);
sw.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + ", saving as " + sOutputFile);
oWord.ActiveDocument.SaveAs2(sOutputFile, FileFormat: 10);
oWord.Quit();
oWord = null;
sw.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + ", Done");
}
catch (Exception e) {
sw.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + ", Error :"+ e.Message);
}
finally
{
sw.Close();
Console.WriteLine("Done");
}
}
}
}
【问题讨论】: