Click here to Skip to main content
Click here to Skip to main content

Printing Word Documents in C#

, 15 Jul 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

tartanmonkey
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Questionsolution for printing of all files. Pinmembernsk_saravana7-Dec-11 3:01 
Questioncode to print xls Pinmembernsk_saravana6-Dec-11 2:16 
QuestionMy code in vb.net Pinmembernsk_saravana5-Dec-11 22:23 
GeneralMy vote of 5 PinmembervickiD15-Sep-10 20:04 
GeneralPrinting Word Document as XPS PinmemberMember 248948423-Jan-09 0:24 
GeneralThermal Printer PinmemberNewbieWizDev5-Nov-08 6:21 
GeneralVery goog! PinmemberJansen2084-Sep-08 23:23 
GeneralPrinting to PDF Pinmembernano2k4-Jun-08 23:19 
Hi
 
Your code helped me.
Anyway, I want to print to PDF.
I already have my Acrobat Pro installed.
To print to PDF, I set the printer to Abobe PDF, I open the document and print.
The code is as follows:
 
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
 
namespace PdfPrintTest {
    public partial class Form1 : Form {
        public Form1 () {
            InitializeComponent();
        }
 
        private object oMissing = System.Reflection.Missing.Value;
        private object oDocFileName = @"C:\SB3_document.doc";
        private object oTrue = true;
        private object oFalse = false;
 
        private void button1_Click (object sender, EventArgs e) {
            object oPrintFileName = Path.ChangeExtension(oDocFileName.ToString(), ".pdf");
            Word.Application app = new Word.ApplicationClass();
            try {
                Word.Document doc = app.Documents.Open(ref oDocFileName, 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);
                app.ActivePrinter = "Adobe PDF";
                doc.PrintOut(ref oTrue, ref oFalse, ref oMissing, ref oPrintFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            }
            finally {
                ((Word._Application)app).Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }
    }
}
 
The problem is that the resulting PDF file is not a valid one (that is, cannot be open with Acrobat reader). For one of my documents, the resulting file has approx. 120KB Sniff | :^)
It's strange because if I printed the file by opening the file and clicking "Print" would produce a 7KB file OMG | :OMG: that is perfectly recognized...
 
Any help would be greatly appreciated...
Thanks.
GeneralRe: Printing to PDF PinmemberJoshDunke11-Jun-09 6:10 
GeneralRe: Printing to PDF Pinmembernano2k11-Jun-09 6:16 
GeneralWord assembly PinmemberCobus C29-Mar-07 1:27 
GeneralRe: Word assembly Pinmembervrumfundel30-Mar-07 0:03 
GeneralRe: Word assembly PinmemberCobus C30-Mar-07 2:19 
GeneralRe: Word assembly Pinmemberap_wooka2-Apr-08 11:21 
GeneralCorrección del código Pinmemberkateerre7-Oct-05 1:09 
GeneralRe: Corrección del código PinmemberStephengb11-Feb-06 14:18 
GeneralShort and good Pinmembersupergiovine24-Aug-05 3:03 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Jul 2005
Article Copyright 2005 by tartanmonkey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid