Click here to Skip to main content
11,408,708 members (61,317 online)
Click here to Skip to main content

Printing a Word Document using Word Automation

, 9 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Shows a simple way to print a Word document


I began my journey down Word Automation and found good articles here. Unfortunately, I hit a big road block: I couldn't choose which printer to print to. I could only print to whichever printer was the ActivePrinter when the Word object was created. Everywhere I looked I was told that all you had to do was:

WordApp.ActivePrinter = "Printer name"

This didn't work for me, it would just hang. I believe that this is due to different versions, and some might have never had the problem I did, but if you're like me, then read on and you won't have to deal with all the headache I had. Hope it helps.


Word Automation is simply being able to use another application within another. Sounds simple enough, but it can get ugly pretty fast and I have found very few resources for when you hit a problem.

Using the Code

Attached is a VB.NET project that allows you to print a specified Word document. The code is given here.

Make sure to add a reference to the Word Object Library found in the COM tab. (To add a reference, right-click on project and select Add Reference.)

Declare variables on show file and print dialogs to get user input:

Dim f As New OpenFileDialog
Dim p As New PrintDialog
Dim app As Word.Application
Dim doc As Word.Document

'Open file and print dialogs to get desired document and printer
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
     If p.ShowDialog = Windows.Forms.DialogResult.OK Then

Create a Word application object:

 'Create instance of Word Application
                app = New Word.Application

This is where you need to use the WordBasic property to set the active printer.

'Set Printer
 app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, _

The rest of the code simply opens the document, prints it, and quits the Word application:

        'Set filename to object type
        Dim filename As Object = f.FileName
        Dim m As Object = System.Reflection.Missing.Value

        'Open document
        doc = app.Documents.Open(filename, m, m, m, m, m, m, m, m, m, m, m)

        'Print document

        'Close document

        'Quit word application

        app = Nothing
    End If
End If 

The most important line in the above code is this:

'Set Printer
app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, _

For those who need the above in C#, here it is:

//Set Printer 
object wb = app.WordBasic;

//first arg is a printer name
object[] argValues = new object[] {p.PrinterSettings.PrinterName, 1}; 
String [] argNames = new String [] {"Printer","DoNotSetAsSysDefault"};


I hope this helps people, because it really got me out of a bind.


  • 9th June, 2007: Initial post


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


About the Author


United States United States
No Biography provided

Comments and Discussions

GeneralWonderful! PinmemberJohnAndre7-May-14 5:38 
QuestionGOOD JOB! But have one question PinmemberMember 1032105913-Oct-13 17:50 
QuestionGood Article josepaulino....but haiving One Que ? Pinmemberkoolprasad20038-Oct-10 21:33 
GeneralMy vote of 5 Pinmemberkoolprasad20038-Oct-10 21:26 
GeneralThanks alot PinmemberDurgaPNaidu21-Nov-07 2:11 
GeneralA small caveat PinmemberJorge Varas12-Jun-07 9:20 
GeneralRe: A small caveat Pinmemberdazzer12320-Aug-07 2:38 
GeneralMultiple Versions PinmemberStrattonN12-Jun-07 4:59 
AnswerRe: Multiple Versions Pinmemberjosepaulino12-Jun-07 5:12 
GeneralRe: Multiple Versions PinmemberHirenjPatel2-Aug-07 1:49 
GeneralRe: Multiple Versions Pinmemberjosepaulino2-Aug-07 4:25 
GeneralRe: Multiple Versions PinmemberMember 98881887-Apr-14 3:01 
GeneralWord user confirmations Pinmemberjportelas12-Jun-07 4:48 
GeneralRe: Word user confirmations [modified] PinmemberStrattonN12-Jun-07 5:07 
GeneralRe: Word user confirmations Pinmemberjosepaulino12-Jun-07 5:19 
GeneralRe: Word user confirmations PinmemberNicolas MEURET14-Jun-07 7:08 
AnswerRe: Word user confirmations Pinmemberjosepaulino14-Jun-07 8:05 
GeneralRe: Word user confirmations PinmemberBeatrice Monk23-Oct-14 0:40 

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
Web03 | 2.8.150414.5 | Last Updated 9 Jun 2007
Article Copyright 2007 by josepaulino
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid