通过任务计划程序运行Office Interop [英] Office Interop run via Task Scheduler

查看:66
本文介绍了通过任务计划程序运行Office Interop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试设置c#控制台程序,该程序使用Word作为中介将pdf转换为html.登录时可从命令行运行,但从调度程序运行时将无法运行.现在,我知道MS不正式支持此功能-

Trying to set up c# console program that converts pdf to html using Word as intermediary. Works from command line when logged in, but will not do when run from scheduler. Now I know MS do not officially support this- https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office - but no the less I wish to pursue this avenue. I have seen various posts on workarounds, but have failed to get it to work.

简而言之,程序会在打开文档时停顿.

In short program stalls at the point of opening the document.

任何人都可以建议采取哪些步骤来解决此MS实施的限制.

Can anyone advise on what steps can be taken to workaround this MS implemented restriction.

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");
            }
        }
    }
}

推荐答案

据我了解,没有解决方法.

As far as I understand there are no workarounds.

Office Interop(和其他依赖GUI的库)仅在交互式桌面会话中可以正常工作.

Office Interop(and other GUI-dependent libraries) will work normally only in the interactive desktop session.

还有

  1. https://serverfault.com/questions/458848/can-i-schedule-a-windows-task-to-run-in-an-interactive-desktop-session
  2. 说没有任何间接选择可以实现它.

    say there aren't any indirect options to achieve it.

    所以你应该

    1. 以当前登录的用户身份运行/安排应用程序.
    2. 或忍着硬着头皮,转而使用OpenXML/另一种替代方法,如克里斯托弗的答案提出的.

    这篇关于通过任务计划程序运行Office Interop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆