How to print a RTF format document via MS-Word using .NET framework

This article is written with the intention of helping someone that also wants to print an RTF format document via MS-Word using the .NET framework. The solution is written as methods in C#. I have found several articles at different forums, etc... but none that illustrates the entire process. The solution has been tested with MS-Office 2007.

As the first step, please add a reference to .NET assembly Microsoft.Office.Interop.Word, since that's where the MS-Word methods are located.

Make sure to include the namespace declaration:

using Microsoft.Office.Interop.Word;


The method(s) illustrated below are static methods in a class.

Reasonable defaults have been passed to the method, but many options are available. I encourage the reader to explore the parameters passed to methods Documents.Open and ActiveDocument.PrintOut to discover the other posibilities.

/// <summary>

/// Print RTF document via MS-Word.

/// The document is printed to the default printer.

/// </summary>

/// <param name="fullFileName">full-path file name of the RTF document</param>

/// <param name="errorMessage">error message if opening/printing failed</param>

/// <returns>true if successful</returns>

public static bool PrintDocumentViaMSWord(object fullFileName, out string errorMessage)

{

bool isSuccessful = true;

errorMessage = null;

// object values for COM interface

object objTrue = true;

object objFalse = false;

object missingValue = Type.Missing;

object format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF;

// load MS-Word application framework (this step initially takes about 1/2 second)

ApplicationClass wordFramework = new ApplicationClass();

Microsoft.Office.Interop.Word.Application wordApplication = wordFramework.Application;

// do not display UI alerts/messages

wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

try

{

// attempt to open the specified document

Document document = wordApplication.Documents.Open(ref fullFileName, ref objTrue

, ref objFalse, ref objFalse, ref missingValue, ref missingValue

, ref objTrue, ref missingValue, ref missingValue, ref format

, ref missingValue, ref missingValue, ref missingValue, ref missingValue

, ref missingValue, ref missingValue);

// check whether the document was opened ...

// (if the document failed to open, this step causes an exception)

object openDocument = wordFramework.ActiveDocument;

// print document

isSuccessful = PrintOpenDocument(ref objFalse, ref missingValue

, wordApplication, document, ref errorMessage);

// close document

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

}

catch (Exception e1)

{

errorMessage = e1.ToString();

isSuccessful = false;

}

// always release resources (this step is very important)

wordApplication.Quit(ref missingValue, ref missingValue, ref missingValue);

return isSuccessful;

}

/// <summary>

/// Print Open MS-Word document

/// </summary>

/// <param name="objFalse">false value</param>

/// <param name="missingValue">missing value</param>

/// <param name="wordApplication">application instance</param>

/// <param name="document">document to print</param>

/// <param name="errorMessage">error message if printing failed</param>

/// <returns>true if successful</returns>

private static bool PrintOpenDocument(ref object objFalse, ref object missingValue

, Microsoft.Office.Interop.Word.Application wordApplication

, Document document, ref string errorMessage)

{

bool isSuccessful = true;

try

{

// print opened document

wordApplication.ActiveDocument.PrintOut(ref objFalse, ref objFalse,

ref missingValue, ref missingValue, ref missingValue, ref missingValue

, ref missingValue, ref missingValue, ref missingValue, ref missingValue,

ref objFalse, ref missingValue, ref missingValue, ref missingValue,

ref missingValue, ref missingValue, ref missingValue, ref missingValue);

// wait for printing to complete

int maxWaitSeconds = 20;

while (wordApplication.BackgroundPrintingStatus > 0)

{

System.Threading.Thread.Sleep(1000);

if (0 == maxWaitSeconds--)

{

errorMessage = "Error, printing to printer did not complete within 20 seconds";

isSuccessful = false;

break;

}

}

}

catch (Exception e1)

{

errorMessage = e1.ToString();

isSuccessful = false;

}

return isSuccessful;

}


By Nalin Jayasuriya   Popularity  (2233 Views)
Biography - Nalin Jayasuriya
Senior software architect/developer with over 25 years of experience in I/T. Working with .NET frameworks since late 2002.