0

我在用:

  • 办公室 2007
  • VC# 速递 2010
  • 1x Citrix 虚拟 XP 网络环境通过 Windows 7 笔记本电脑主机访问
  • 1x 打印机设置为在给定的网络映射驱动器中输出到 .prn

我正在使用 C# 和 Word Interop 以静默方式自动打印一组给定的文件。该应用程序每 10 分钟扫描一次输入文件夹,仅查找 .doc / .docx 文件,并将它们的路径和文件名输入到列表中。Foreach 找到的文件,尝试通过以下代码打印:

public static Boolean PrintFoundFiles(List<string> foundFiles)
    {
        successful = false;
        foreach (string file in foundFiles)
        {
            object fileAndPath = file;              //declare my objects here, since methods want an object passed
            object boolTrue = true;                 //
            object boolFalse = false;               //
            object output = FormatOutputName(file); //
            object missing = System.Type.Missing;   //
            object copies = "1";                    //
            object pages = "";                      //
            object items = Word.WdPrintOutItem.wdPrintDocumentContent; //
            object range = Word.WdPrintOutRange.wdPrintAllDocument;    //
            object pageType = Word.WdPrintOutPages.wdPrintAllPages;    //

            Word.Application wordApp = new Word.Application(); //open word application
            wordApp.Visible = false; //invisible
            Word.Document doc = wordApp.Documents.Open(ref fileAndPath, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                       ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                       ref missing, ref missing, ref missing); //opens the word document into application behind the scenes
            doc.Activate(); //activates document, else when I print the system will throw an exception
            wordApp.ActivePrinter = "my printer name"; //Specify printer I will print from
            doc.PrintOut(ref boolTrue, ref boolFalse, ref range, ref output, ref missing, ref missing,
                ref items, ref copies, ref pages, ref pageType, ref boolTrue, ref boolTrue,
                ref missing, ref boolFalse, ref missing, ref missing, ref missing, ref missing);
            doc.Close(SaveChanges: false);
            doc = null;
            ((Word._Application)wordApp).Quit(SaveChanges: false); //kill word process the right way
            wordApp = null; //reset to null
            successful = true;
        }
        return successful;
    }

在我收到“成功”的真正布尔值后,我会自动将文件备份到备份文件夹中,在输入文件夹中将其删除,并在输出文件夹中查找 .prn(它只是坐在这里以供稍后处理)。

如果我不提供输出(请参阅 doc.PrintOut() 上的参考输出),则输出目录根本不会更新或打印到。如果我确实提供了输出,则会创建 .prn,尽管它是一个 0kb 的空文件。

该打印机被设置为默认打印机,并已配置为自动输出到所述输出文件夹。如果我用我试图自动打印的同一个文件手动打开 Word,点击打印,它将在输出目录中创建一个 6kb 的 .prn 文件,而无需点击 CTRL + P 以外的任何东西,OK。

我相当有信心通过“Word.Document doc = wordApp.Documents.Open()”打开该文件,因为我做了一个 doc.FullName 并获得了相关输入 word 文档的完整路径。我只是无法让 .prn 正确输出到输出文件夹。

4

1 回答 1

1

如果我开始我的词(2010)并记录我按下 Ctrl+P 并点击打印的宏 - 我得到

Application.PrintOut fileName:="", Range:=wdPrintAllDocument, Item:= _
    wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
    wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
    PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

更改您的 PrintOut 以反映 Word 所做的事情,看看它是否能解决您的问题。

没有理由“相当自信”,只需删除

wordApp.Visible = false

调试您的程序并确保它正常。

于 2013-10-01T13:21:52.190 回答