Search code examples
c#.netprintingprocessstartinfo

Possible Issue with ProcessStartInfo and Winword?


This code works, in that the document is successfully printed to the default printer.

    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);

    }

Problem: The info.Arguments just does not pick them up at all. The "/q" should stop the splash screen. I have tested this separately using

    public static void OpenMSWord()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "WINWORD.EXE";
        startInfo.Arguments = "/q /n";
        Process.Start(startInfo);


    }

This processes the arguments. The "/q" stops the little winword pop up window during the loading, as expected.

I am using the .Net 4.0 framework. This is being done as a console application.

Is there something simple I am missing here ?

Thanks in advance for any help or suggestions you may have on this.


Solution

  •  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);
                }
    
    
            }
    

    Where fiObject is a document path. fi.FullName can be replaced with a file location string.

    After a ton of searching online I found the above to provide what I want. That is Microsoft word does not open and documents are sent to the printer.

    The code is based off this blog. Credit to this guy. http://wurstkoffer.wordpress.com/2013/05/18/c-printing-to-word-programmatically-in-3-way/ Method #3 is what I have followed.

    Edit 1 **The 1st argument in the doc.PrintOut() is "Background" which is the Micrsoft website describes as "true to have the customization code continue while Microsoft Office Word prints the document." In other words the method PrintOut will stop your code executing until the document has been sent to the printer. A little pop up will appear. This is very important if you have a loop for printing multiple documents in order to give them time to process.