Word Interop 2007静音打印问题

时间:2013-10-01 12:06:18

标签: c# ms-word interop

我正在使用:

  • Office 2007
  • VC#Express 2010
  • 通过Windows 7笔记本电脑主机访问的1x Citrix虚拟XP网络环境
  • 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()上的ref输出),则输出目录根本不会更新或打印。如果我提供输出,则会创建.prn,尽管它是一个0kb的空文件。

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

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

1 个答案:

答案 0 :(得分: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

调试程序并确定它没问题。