C# – Printing to Microsoft Word programmatically in 3 ways

1st method – with dialog to be answered
2nd method – with dialog automatically closed
3rd method – without dialog

Code Snippet
  1. string filename = @”C:\test.docx”;
  2. string printer = “Brother DCP-7010 USB Printer”;
  3.  
  4.  
  5. // 1st method – with dialog to be answered
  6.  
  7. // Send it to the selected printer
  8. using (PrintDialog printDialog1 = new PrintDialog())
  9. {
  10.   printDialog1.PrinterSettings.PrinterName = printer;
  11.   if (printDialog1.ShowDialog() == DialogResult.OK)
  12.   {
  13.     System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filename);
  14.     //info.Arguments = “\”” + printDialog1.PrinterSettings.PrinterName + “\””;
  15.     info.Arguments = “\”” + printer + “\””;
  16.     info.CreateNoWindow = true;
  17.     info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  18.     info.UseShellExecute = true;
  19.     info.Verb = “PrintTo”;
  20.     System.Diagnostics.Process.Start(info);
  21.   }
  22. }
  23.  
  24. // 2nd method – with dialog automatically closed
  25.  
  26. ProcessStartInfo info2 = new ProcessStartInfo(filename);
  27. info2.Verb = “Print”;
  28. info2.CreateNoWindow = true;
  29. info2.WindowStyle = ProcessWindowStyle.Hidden;
  30. Process.Start(info2);
  31.  
  32. // 3rd method – without dialog
  33. // you need the “Redistributable Primary Interop Assemblies”
  34. // – which can be downloaded from microsoft website
  35. // – tested with office 2007 (http://www.microsoft.com/en-us/download/details.aspx?id=18346)
  36. // – add assembly reference to project, use “Microsoft.Office.Interop.Word” from GAC (C:\Windows\assembly)
  37.  
  38. Microsoft.Office.Interop.Word.Application wordInstance = new Microsoft.Office.Interop.Word.Application();
  39. //MemoryStream documentStream = getDocStream();
  40. FileInfo wordFile = new FileInfo(filename);
  41. object fileObject = wordFile.FullName;
  42. object oMissing = System.Reflection.Missing.Value;
  43. 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);
  44. doc.Activate();
  45. doc.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
C# – Printing to Microsoft Word programmatically in 3 ways

One thought on “C# – Printing to Microsoft Word programmatically in 3 ways

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s