Click here to Skip to main content
Licence CPOL
First Posted 15 Jul 2005
Views 79,718
Bookmarked 22 times

Printing Word Documents in C#

By | 15 Jul 2005 | Article
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

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

About the Author

tartanmonkey

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionsolution for printing of all files. Pinmembernsk_saravana2:01 7 Dec '11  
Questioncode to print xls Pinmembernsk_saravana1:16 6 Dec '11  
QuestionMy code in vb.net Pinmembernsk_saravana21:23 5 Dec '11  
GeneralMy vote of 5 PinmembervickiD19:04 15 Sep '10  
GeneralPrinting Word Document as XPS PinmemberMember 248948423:24 22 Jan '09  
GeneralThermal Printer PinmemberNewbieWizDev5:21 5 Nov '08  
GeneralVery goog! PinmemberJansen20822:23 4 Sep '08  
GeneralPrinting to PDF Pinmembernano2k22:19 4 Jun '08  
GeneralRe: Printing to PDF PinmemberJoshDunke5:10 11 Jun '09  
GeneralRe: Printing to PDF Pinmembernano2k5:16 11 Jun '09  
GeneralWord assembly PinmemberCobus C0:27 29 Mar '07  
GeneralRe: Word assembly Pinmembervrumfundel23:03 29 Mar '07  
GeneralRe: Word assembly PinmemberCobus C1:19 30 Mar '07  
GeneralRe: Word assembly Pinmemberap_wooka10:21 2 Apr '08  
GeneralCorrección del código Pinmemberkateerre0:09 7 Oct '05  
GeneralRe: Corrección del código PinmemberStephengb13:18 11 Feb '06  
GeneralShort and good Pinmembersupergiovine2:03 24 Aug '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Jul 2005
Article Copyright 2005 by tartanmonkey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid