Click here to Skip to main content
15,868,016 members
Articles / Productivity Apps and Services / Microsoft Office
Article

Printing Word Documents in C#

Rate me:
Please Sign up or sign in to vote.
3.63/5 (25 votes)
15 Jul 2005CPOL 171.2K   27   18
Printing documents using Microsoft Word and C#

Introduction

I feel I need to share this pain. I have been struggling for days now to get something simple to work for me. That is to print out a Word document (RTF format) to a printer using Word. It also has to work with multiple versions of Word (so far 2000 and 2003). I thought I would share everything that I have learnt.

If you want this to work for multiple versions, install the earliest version of Word and Add Reference to its COM component and develop against these.

Next to the code...

The Code

C#
// Create an Application object
Word.ApplicationClass ac = new Word.ApplicationClass();
Word.Application app = ac.Application;

// I'm setting all of the alerts to be off as I am trying to get
// this to print in the background
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

// Open the document to print...
object filename = "myFile.rtf";
object missingValue = Type.Type.Missing;

// Using OpenOld so as to be compatible with other versions of Word
Word.Document document = app.Documents.OpenOld(ref filename,
ref missingValue, ref missingValue, 
ref missingValue, ref missingValue, ref missingValue, 
	ref missingValue, ref missingValue, ref missingValue, ref missingValue);

// Set the active printer
app.ActivePrinter = "My Printer Name";

object myTrue = true; // Print in background
object myFalse = false;

// Using PrintOutOld to be version independent
m_App.ActiveDocument.PrintOutOld(ref myTrue, 
ref myFalse, ref missingValue, ref missingValue, ref missingValue, 
	missingValue, ref missingValue, 
ref missingValue, ref missingValue, ref missingValue, ref myFalse, 
	ref missingValue, ref missingValue, ref m_MissingValue);

document.Close(ref missingValue, ref missingValue, ref missingValue);

// Make sure all of the documents are gone from the queue
while(m_App.BackgroundPrintingStatus > 0)
{
System.Threading.Thread.Sleep(250);
}

app.Quitref missingValue, ref missingValue, ref missingValue);

I do not claim that this is perfect, but it's as good as I can get. It works on my machine anyway.

History

  • 15th July, 2005: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionsolution for printing of all files. Pin
nsk_saravana7-Dec-11 2:01
nsk_saravana7-Dec-11 2:01 
Questioncode to print xls Pin
nsk_saravana6-Dec-11 1:16
nsk_saravana6-Dec-11 1:16 
QuestionMy code in vb.net Pin
nsk_saravana5-Dec-11 21:23
nsk_saravana5-Dec-11 21:23 
GeneralMy vote of 5 Pin
vickiD15-Sep-10 19:04
vickiD15-Sep-10 19:04 
GeneralPrinting Word Document as XPS Pin
Member 248948422-Jan-09 23:24
Member 248948422-Jan-09 23:24 
GeneralThermal Printer Pin
NewbieWizDev5-Nov-08 5:21
NewbieWizDev5-Nov-08 5:21 
GeneralVery goog! Pin
Jansen2084-Sep-08 22:23
Jansen2084-Sep-08 22:23 
GeneralPrinting to PDF Pin
nano2k4-Jun-08 22:19
nano2k4-Jun-08 22:19 
GeneralRe: Printing to PDF Pin
JoshDunke11-Jun-09 5:10
JoshDunke11-Jun-09 5:10 
GeneralRe: Printing to PDF Pin
nano2k11-Jun-09 5:16
nano2k11-Jun-09 5:16 
GeneralWord assembly Pin
Cobus C29-Mar-07 0:27
Cobus C29-Mar-07 0:27 
GeneralRe: Word assembly Pin
vrumfundel29-Mar-07 23:03
vrumfundel29-Mar-07 23:03 
GeneralRe: Word assembly Pin
Cobus C30-Mar-07 1:19
Cobus C30-Mar-07 1:19 
GeneralRe: Word assembly Pin
ap_wooka2-Apr-08 10:21
ap_wooka2-Apr-08 10:21 
GeneralCorrección del código Pin
kateerre7-Oct-05 0:09
kateerre7-Oct-05 0:09 
GeneralRe: Corrección del código Pin
Stephengb11-Feb-06 13:18
Stephengb11-Feb-06 13:18 
GeneralShort and good Pin
Pierfabio24-Aug-05 2:03
Pierfabio24-Aug-05 2:03 
GeneralMessage Closed Pin
12-Jul-15 21:12
Pokenderyindo12-Jul-15 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.