string envelope_template = "temp.doc";
            string envelope_printer = "printer";  //this is the destination printer
            string invoice_printer = "printer"; //this is the default printer            Word._Application wrdApp = null;
            Word._Document wrdDoc = null;
            Word.MailMerge wrdMailMerge;            object oMissing = System.Reflection.Missing.Value;
            object oFalse = false;
            object oTrue = true;
            object oNull = String.Empty;            try
            {
                wrdApp = new Word.Application();
                wrdApp.Visible = false;
                wrdApp.ChangeFileOpenDirectory(Directory.GetCurrentDirectory());
                object oEnvelopeTemplate = envelope_template;
                wrdDoc = wrdApp.Documents.Add(ref oEnvelopeTemplate, ref oMissing,
                    ref oMissing, ref oMissing);
                wrdMailMerge = wrdDoc.MailMerge;                wrdMailMerge.OpenDataSource(
                   "wenben.txt",
                    ref oMissing, ref oMissing, ref oFalse, ref oTrue, ref oFalse,
                    ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                wrdApp.ActivePrinter = envelope_printer;
                wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter;
                wrdMailMerge.SuppressBlankLines = true;
                wrdMailMerge.Execute(ref oFalse);
                wrdApp.ActivePrinter = invoice_printer;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MailMerge:" + "" + ex.Message);
            }
            finally
            {
                if (wrdDoc != null)
                {
                    wrdDoc.Saved = false;
                    wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
                    wrdDoc = null;
                }                if (wrdApp != null)
                {
                    wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing);
                    wrdApp = null;
                }                wrdMailMerge = null;                GC.Collect();
                GC.WaitForPendingFinalizers();                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

解决方案 »

  1.   

    代码如上,这样打印机中的任务名称是默认的word文档,我想把打印机中的任务显示我想要的Name,怎么做?
      

  2.   

    关注,我也这样做的模板,每次打印不同的文章,打印机队列里都叫microsoft word ,只是大小不同来区分……
    为什么你要
    GC.Collect();
    GC.WaitForPendingFinalizers();
    2次?