【问题标题】:Office Interop run via Task SchedulerOffice 互操作通过任务计划程序运行
【发布时间】: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");
            }
        }
    }
}

【问题讨论】:

    标签: c# interop scheduler


    【解决方案1】:

    根据您使用的格式,有 3 种基本情况。

    • 如果您只需要现代格式(如 docx),您可以使用OpenXML SDK。其他选项包括它的任何包装器,甚至是 ZipArchive 和 XMLreader 类 - 现代格式非常简单且众所周知。
    • 如果您还需要支持旧格式(如 .doc),主要方式是 (t)rusty Office COM Interop。它会受到 COM 互操作的所有正常限制,需要Interative Session 和 Office 安装才能工作。
    • 对于任何给定的格式、任何给定的操作和任何给定的前端(WinForms、ASP.Net),都可能有第三种处理方式。但这些都介于两者之间,不能依赖。

    我的建议是始终使用 OpenXML SDK。尝试埋葬支持旧格式的想法,只将旧文件转换为新格式。

    Interop 已经过时并且有很多额外的问题。但正如我们一直将其作为后备,.NET 从来没有一个完整的库来阅读和处理旧的办公格式。

    我的猜测是您缺少交互式会话。您可以强制 TaskSheduler 为您提供交互式会话。毕竟,没有像服务那样​​的安全需求。但我仍然建议您开始采用旧学校的方式,只选择现代格式。旧的确实比他们的时代更长寿。

    【讨论】:

    • 用例是将一组pdf和docx转换为单个epub。使用 MS word 作为中介来创建 html 完成了足够体面的转换工作。我不清楚 OpenXML SDK 如何将 pdf 转换为 word。
    • @faeskene 我怀疑 OpenXMl 可以做到这一点。这听起来像是罕见的 Office/Interop 唯一功能。这将使您以另一种方式解决此问题,或者使该任务成为交互式会话。选择你的毒药:)
    【解决方案2】:

    据我所知,没有变通办法。

    Office Interop(和其他依赖于 GUI 的库)只能在交互式桌面会话中正常工作。

    作为

    1. https://serverfault.com/questions/458848/can-i-schedule-a-windows-task-to-run-in-an-interactive-desktop-session
    2. A workaround for the fact that a scheduled task in Windows requires a user to be logged in

    说没有任何间接的选择可以实现它。

    所以你应该要么

    1. 以当前登录的用户身份运行/调度应用程序。
    2. 或者硬着头皮切换到 OpenXML/Christopher's answer 建议的另一种替代方案。

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 2021-01-21
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2012-08-15
      • 2017-06-07
      相关资源
      最近更新 更多