【问题标题】:Possible Issue with ProcessStartInfo and Winword?ProcessStartInfo 和 Winword 的可能问题?
【发布时间】:2014-08-06 17:13:25
【问题描述】:

此代码有效,因为文档已成功打印到默认打印机。

    public static void OpenMSWord()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "WINWORD.EXE";
        startInfo.Arguments = "/q /n";
        startInfo.Verb = "Print";
        startInfo.FileName = "C:\\Test Page.docx";
        Process.Start(startInfo);

    }

问题: info.Arguments 根本不接受它们。 “/q”应该停止启动画面。我已经使用

单独测试了这个
    public static void OpenMSWord()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "WINWORD.EXE";
        startInfo.Arguments = "/q /n";
        Process.Start(startInfo);


    }

这会处理参数。 正如预期的那样,“/q”在加载过程中停止了小winword弹出窗口。

我正在使用 .Net 4.0 框架。 这是作为控制台应用程序完成的。

我在这里缺少什么简单的东西吗?

提前感谢您对此提供的任何帮助或建议。

【问题讨论】:

    标签: c# .net printing processstartinfo


    【解决方案1】:

    您应该将文件名作为参数的一部分。

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "WINWORD.EXE";
    startInfo.Arguments = "/q /n C:\\Test Page.docx";
    startInfo.Verb = "Print";
    Process.Start(startInfo);
    

    您正在用要打开的文档覆盖可执行文件名称。 Process.Start 与文档一起工作,因为 docx 文件和 winword 之间的文件关联,但将忽略 Arguments

    【讨论】:

    • 感谢您的回复。不幸的是,您的答案中的代码返回了以下错误。{“No application is associated with the specified file for this operation”} 我试过 startInfo.Arguments = "C:\\Test Page.docx";这也会报错。
    【解决方案2】:
     public static void printThread(object fiObject)
            {
    
                FileInfo fi = (FileInfo)fiObject;
                try
                {
    
                    Microsoft.Office.Interop.Word.Application wordInstance = new Microsoft.Office.Interop.Word.Application();
                    //MemoryStream documentStream = getDocStream(); 
                    FileInfo wordFile = new FileInfo(fi.FullName);
                    object fileObject = wordFile.FullName;
                    object oMissing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word.Document doc = wordInstance.Documents.Open(ref fileObject, 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, ref oMissing);
                    doc.Activate();
                    doc.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                    Console.WriteLine("Printed " + fi.FullName);
                }
    
                catch (Exception ex)
                {
                    Console.WriteLine("Error:  " + ex);
                }
    
    
            }
    

    fiObject 是文档路径。 fi.FullName 可以替换为文件位置字符串。

    经过大量在线搜索后,我发现上面提供了我想要的东西。即 Microsoft word 无法打开,文档被发送到打印机。

    代码基于此博客。归功于这个家伙。 http://wurstkoffer.wordpress.com/2013/05/18/c-printing-to-word-programmatically-in-3-way/ 方法#3是我遵循的。

    编辑 1 ** doc.PrintOut() 中的第一个参数是“背景”,这是 Micrsoft 网站描述的 “在 Microsoft Office Word 打印文档时继续自定义代码是正确的。” 换句话说,方法 PrintOut 将停止您的代码执行,直到文档被发送到打印机。会出现一个小弹出窗口。如果您有一个循环打印多个文档以便让它们有时间处理,这一点非常重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-26
      • 2011-06-10
      • 2014-08-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多