Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Welcome to this tutorial, where we will explore how to print Word documents using the Microsoft Interop in a C# Console Application. This beginner-friendly guide will walk you through the steps to programmatically print Microsoft Word documents.
Before diving into the code, it's essential to have a few things set up:
Microsoft Word Installation: Ensure you have Microsoft Word installed on your system. If not, head to your computer's official Microsoft website or the app store to install it.
Visual Studio Setup: You should have Visual Studio installed with the capability to create a Console Application. If you're a beginner, consider downloading Visual Studio Community, which is free and sufficient for our needs.
Using Interop requires a reference to the Microsoft Office Interop library. Here's how to add it:
You can also install it using the NuGet Package Manager.
Ensure that your application's target framework is compatible with the Interop library. You can check this by right-clicking on your project in Solution Explorer, selecting Properties, and then viewing the Target framework under the Application tab. If you face issues with the Interop library's version, consider downloading the necessary package or assembly or adjusting the target framework version.
With the environment set up, you can now proceed with the coding process.
The document object is at the heart of the Interop services when dealing with Word documents. This object represents a Microsoft Word document and provides all its functionalities.
A common task is opening a document:
using Word = Microsoft.Office.Interop.Word;
// Object needed to avoid passing specific parameters
object oMissing = Type.Missing;
// File path to the Word document you want to open
object fileName = @"C:\path_to_document\document.docx";
// Create a new instance of the Word application
Word.Application wordApp = new Word.Application();
// Open the document with specified parameters
Word._Document wordDoc = wordApp.Documents.Open(
ref fileName, 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);
using Word = Microsoft.Office.Interop.Word;
// Object needed to avoid passing specific parameters
object oMissing = Type.Missing;
// File path to the Word document you want to open
object fileName = @"C:\path_to_document\document.docx";
// Create a new instance of the Word application
Word.Application wordApp = new Word.Application();
// Open the document with specified parameters
Word._Document wordDoc = wordApp.Documents.Open(
ref fileName, 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);
Imports Word = Microsoft.Office.Interop.Word
' Object needed to avoid passing specific parameters
Private oMissing As Object = Type.Missing
' File path to the Word document you want to open
Private fileName As Object = "C:\path_to_document\document.docx"
' Create a new instance of the Word application
Private wordApp As New Word.Application()
' Open the document with specified parameters
Private wordDoc As Word._Document = wordApp.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)
The multiple parameters with ref oMissing
might seem daunting, but it's essential for the Open method, which expects numerous arguments, most of which are optional.
With our environment set up and understanding the document object, it's time to dive into the core functionality of printing Word documents.
To print the document, you can use the following method:
// Method to print the document using default printer settings
private void ButtonPrint_Click(object sender, EventArgs e)
{
wordDoc.PrintOut(); // Sends the document to the default printer
}
// Method to print the document using default printer settings
private void ButtonPrint_Click(object sender, EventArgs e)
{
wordDoc.PrintOut(); // Sends the document to the default printer
}
' Method to print the document using default printer settings
Private Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
wordDoc.PrintOut() ' Sends the document to the default printer
End Sub
This method sends the document to the default printer using the default settings.
If you want to introduce a print dialog, customize printer settings, or even print multiple pages, you'd require a more detailed approach:
// Method to print the document with custom settings
private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
// Number of copies to print
object copies = "1";
// Page range to print, e.g., pages 1 to 3
object pages = "1-3";
// Print the document with specified copies and page range
wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
// Method to print the document with custom settings
private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
// Number of copies to print
object copies = "1";
// Page range to print, e.g., pages 1 to 3
object pages = "1-3";
// Print the document with specified copies and page range
wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
' Method to print the document with custom settings
Private Sub ButtonPrintWithSettings_Click(ByVal sender As Object, ByVal e As EventArgs)
' Number of copies to print
Dim copies As Object = "1"
' Page range to print, e.g., pages 1 to 3
Dim pages As Object = "1-3"
' Print the document with specified copies and page range
wordDoc.PrintOut(Copies:= copies, Pages:= pages)
End Sub
In the above source code, we specify the page range and number of copies, but the potential customizations are vast.
The ability to modify print settings is what sets programmatic control apart. Whether you want to adjust the printer settings, define a specific printer, or even silently print the document, it's all within reach with Interop.
Silent printing is all about sending the document to the printer without any user interactions:
// Object to determine whether to print in the background or not
object background = false;
// Print the document silently (no user interactions)
wordDoc.PrintOut(Background: ref background);
// Object to determine whether to print in the background or not
object background = false;
// Print the document silently (no user interactions)
wordDoc.PrintOut(Background: ref background);
' Object to determine whether to print in the background or not
Dim background As Object = False
' Print the document silently (no user interactions)
wordDoc.PrintOut(Background:= background)
To print a document on a specific printer other than the default:
// Set the active printer to a specified printer by name
wordApp.ActivePrinter = "Printer Name";
// Print the document using the specified printer
wordDoc.PrintOut();
// Set the active printer to a specified printer by name
wordApp.ActivePrinter = "Printer Name";
// Print the document using the specified printer
wordDoc.PrintOut();
' Set the active printer to a specified printer by name
wordApp.ActivePrinter = "Printer Name"
' Print the document using the specified printer
wordDoc.PrintOut()
Beyond just specifying the printer, one might need to adjust the printer settings:
// Creates a PrintDialog to allow the user to choose printer settings
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
// Sets the Word application's active printer to the user's choice
wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
// Prints the document using user's selected printer settings
wordDoc.PrintOut();
}
// Creates a PrintDialog to allow the user to choose printer settings
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
// Sets the Word application's active printer to the user's choice
wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
// Prints the document using user's selected printer settings
wordDoc.PrintOut();
}
' Creates a PrintDialog to allow the user to choose printer settings
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = DialogResult.OK Then
' Sets the Word application's active printer to the user's choice
wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName
' Prints the document using user's selected printer settings
wordDoc.PrintOut()
End If
This way, the user can manually adjust settings like orientation, duplex printing, and more.
While Microsoft Interop provides functionalities to manage Word documents, it's not as robust and efficient as it should be for serious commercial use. Enter IronWord—a superior alternative to Interop for Word DOCX file processing. IronWord allows for seamless reading, writing, and manipulation of Excel files in C#. Learn more about how to get started with IronWord.
In this tutorial, we've delved into the steps involved in leveraging Microsoft Interop to print Word documents programmatically in a C# Console Application. We've seen how to show the print dialog, set custom print settings, and control various printing aspects like choosing a specified printer or defining a page range. While Interop offers foundational capabilities, it's worth noting that there are potent alternatives like IronWord.
You need Microsoft Word installed, Visual Studio set up, and a sample Word document for testing. Alternatively, you can use IronWord for efficient processing of Word documents without needing Microsoft Word installed.
Open Visual Studio, click on 'Create a new project', search for 'Console App', select the C# template, and name your project.
In Visual Studio, right-click on your project, go to Add > Reference, and select 'Microsoft Word xx.x Object Library' under the COM tab. Alternatively, you can use IronWord which does not require COM references.
The Document Object represents a Microsoft Word document and provides functionalities to work with it programmatically. IronWord also provides similar document manipulation capabilities with added efficiency.
Use the method wordDoc.PrintOut() to send the document to the default printer with default settings. Alternatively, IronWord can be used for printing with more flexibility and control.
You can specify parameters like number of copies and page range using the PrintOut method, e.g., wordDoc.PrintOut(Copies: ref copies, Pages: ref pages). IronWord also allows for customizable printing options.
Silent printing allows you to send documents to the printer without user interactions, using wordDoc.PrintOut(Background: ref background) with background set to false. IronWord supports silent printing as well.
Set wordApp.ActivePrinter to the desired printer's name before calling wordDoc.PrintOut(). IronWord provides similar functionality for specifying printers.
IronWord provides more robust and efficient processing of Word documents, offering seamless reading, writing, and manipulation of DOCX files in C#, without the need for Microsoft Word installation.
Use a PrintDialog to allow users to choose printer settings, and set wordApp.ActivePrinter to the selected printer name before printing. IronWord also allows for user-customized print dialogs.