Printing word without opening Application

 

using  System;
using  System.Windows.Forms;
using  Microsoft.CSharp;
using  System.IO;
using  System.Diagnostics;
using  System.Collections;
using  Word;
 

namespace  Printing
{

    
/// <summary>

    
/// Summary description for Printing.

    
/// </summary>


    
public class Printing
    
{

        
public Printing()
        
{


        }


        
public bool Bln_PrintAllFiles(string StrFileName)
        
{
            Word.Application ObjWord 
=new Word.Application(); //Create Word Application

            
object M=Type.Missing; //M is missinghi..hi

            
object V= StrFileName; //Convert to Object..

            
object I=false//Convert to Object

            
try
            
{
                
if(StrFileName.EndsWith(".doc")) //Ends with .doc. itz Word document.
                {
                    ObjWord.Documents.Open(
ref V,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M);

                    ObjWord.PrintOut(
ref I,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M);

                    ObjWord.Quit(
ref M,ref M,ref M);

                    
return true;
                }

                
else //for all other types..I did check for .txt,.pdf and .Msg
                {
                    System.Diagnostics.Process P
=new Process(); //Create a process

                    P.StartInfo.FileName 
= StrFileName.ToString(); //convert .> Readable

                    P.StartInfo.CreateNoWindow 
= true//PerfectCreate No Window

                    P.StartInfo.WindowStyle 
= ProcessWindowStyle.Hidden;//WindowStyle---->HIDDEN

                    P.StartInfo.Verb 
= "print";

                    P.Start(); 
//Start the process

                    P.Dispose();

                    
return true;
                }

            }

            
catch(Exception ee) //Err Handler..
            {
                MessageBox.Show(ee.Message);

                
return false;
            }

        }

    }

}




这是我以上面的例子为基础,写的连续印刷的程序。

        
private   void  btn発行_Click( object  sender, System.EventArgs e)
        
{
            
try
            
{
                crystalReportViewer1.PrintReport();

                
if (MessageProcess.ShowQuestion("チェックシートを印刷してもよろしいですか?"== DialogResult.Yes)
                
{
                    
if (使用=="実績")
                    
{
                    }

                    
else if(動作 == "発行")
                    
{
                        
if(ReportInfoDS.Tables[0].Rows.Count > 0)
                        
{
                            Systematics.Nikuni.BLL.HostSettings bllHostSettings 
= new Systematics.Nikuni.BLL.HostSettings();
                            
string path = bllHostSettings.GetSetValue("チェックシートパス");

                            Word.Application wordApplication 
= (Word.Application)Microsoft.VisualBasic.Interaction.CreateObject("Word.Application","");
                            wordApplication.ScreenUpdating 
= false;

                            
object missing = new object();
                            
object I=false;
                            missing 
= Type.Missing;

                            wordApplication.Documents.Add(
ref missing,ref missing,ref missing,ref missing);
                    
                            
for(int i = 0; i < ReportInfoDS.Tables[0].Rows.Count; i++)
                            
{
                                
if(ReportInfoDS.Tables[0].Rows[i]["チェックシート有無"].ToString() == "")
                                
{
                                    
string filename = ReportInfoDS.Tables[0].Rows[i]["製造部品番号"].ToString();
                                    
string[] dirs = Directory.GetFiles(path,filename+".*");
                            
                                    
foreach(string dir in dirs)
                                    
{                            
                                        
try
                                        
{
                                            
if(dir.EndsWith(".doc"))
                                            
{
                                                
object filenameobject = dir;

                                                wordApplication.PrintOut(
ref I,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref filenameobject,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
                                            }

                                            
else
                                            
{
                                                System.Diagnostics.Process P 
= new System.Diagnostics.Process(); //Create a process

                                                P.StartInfo.FileName 
= dir.ToString(); //convert .> Readable

                                                P.StartInfo.CreateNoWindow 
= true//PerfectCreate No Window

                                                P.StartInfo.WindowStyle 
= ProcessWindowStyle.Hidden;//WindowStyle---->HIDDEN

                                                P.StartInfo.Verb 
= "print";

                                                P.Start(); 
//Start the process

                                                P.Dispose();
                                            }

                                            
//                                System.Diagnostics.Process.Start(dir);
                                        }

                                        
catch(Exception ex)
                                        
{
                                            MessageProcess.ShowError(
"ファイル存在していませんから、確認してください。");
                                        }

                                    }

                                }

                            }
                            
                            
if (wordApplication != null)
                            
{    
                                wordApplication.Quit(
ref missing,ref missing,ref missing);        
                            }

                        }


                    }
    
                }

            }

            
catch (Exception ex)
            
{
                ExceptionManager.Publish(ex);

                MessageProcess.ShowError(
"btn発行_Clickするとき、エラーが発生しました。");
            }
    

        }
                                                                                                                                                                                                                        
                

连续印刷关键在于Printout的第一个参数。

备考参数
object FilePath   = "E:/DirectoryName/FileName.doc";
object False      = false;
object True       = true;
object Blank      = "";
object Format    = Word.WdOpenFormat.wdOpenFormatAuto;
object Encoder   = Office.MsoEncoding.msoEncodingUSASCII;

WordApp.Documents.Open(
   ref FilePath,
   ref False,
   ref False,
   ref False,
   ref Blank,
   ref Blank,
   ref False,
   ref Blank,
   ref Blank,
   ref Format,
   ref Encoder,
   ref True);

你可能感兴趣的:(application)