Click here to Skip to main content
15,878,809 members
Articles / Desktop Programming / Windows Forms
Article

Printing a Word Document using Word Automation

Rate me:
Please Sign up or sign in to vote.
4.43/5 (12 votes)
9 Jun 2007CPOL1 min read 148.8K   3.1K   33   18
Shows a simple way to print a Word document

Introduction

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:

VB.NET
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.

Background

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:

VB.NET
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:

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

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

VB.NET
'Set Printer
 app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, _
    DoNotSetAsSysDefault:=1) 

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

VB.NET
        '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
        app.PrintOut()

        'Close document
        app.Documents.Close()

        'Quit word application
        app.Quit()

        'Release 
        app = Nothing
    End If
End If 

The most important line in the above code is this:

VB.NET
'Set Printer
app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, _
    DoNotSetAsSysDefault:=1) 

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

C#
//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"};

wb.GetType().InvokeMember("FilePrintSetup",BindingFlags.InvokeMethod,null, 
    wb,argValues,null,null,argNames);

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

History

  • 9th June, 2007: Initial post

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWonderful! Pin
JohnAndre7-May-14 4:38
JohnAndre7-May-14 4:38 
QuestionGOOD JOB! But have one question Pin
Member 1032105913-Oct-13 16:50
Member 1032105913-Oct-13 16:50 
QuestionGood Article josepaulino....but haiving One Que ? Pin
koolprasad20038-Oct-10 20:33
professionalkoolprasad20038-Oct-10 20:33 
GeneralMy vote of 5 Pin
koolprasad20038-Oct-10 20:26
professionalkoolprasad20038-Oct-10 20:26 
GeneralThanks alot Pin
DurgaPNaidu21-Nov-07 1:11
DurgaPNaidu21-Nov-07 1:11 
GeneralA small caveat Pin
Jorge Varas12-Jun-07 8:20
Jorge Varas12-Jun-07 8:20 
GeneralRe: A small caveat Pin
dazzer12320-Aug-07 1:38
dazzer12320-Aug-07 1:38 
GeneralMultiple Versions Pin
Nigel Stratton12-Jun-07 3:59
Nigel Stratton12-Jun-07 3:59 
AnswerRe: Multiple Versions Pin
josepaulino12-Jun-07 4:12
josepaulino12-Jun-07 4:12 
GeneralRe: Multiple Versions Pin
hp92-Aug-07 0:49
hp92-Aug-07 0:49 
GeneralRe: Multiple Versions Pin
josepaulino2-Aug-07 3:25
josepaulino2-Aug-07 3:25 
GeneralRe: Multiple Versions Pin
Member 98881887-Apr-14 2:01
Member 98881887-Apr-14 2:01 
GeneralWord user confirmations Pin
jportelas12-Jun-07 3:48
jportelas12-Jun-07 3:48 
GeneralRe: Word user confirmations [modified] Pin
Nigel Stratton12-Jun-07 4:07
Nigel Stratton12-Jun-07 4:07 
GeneralRe: Word user confirmations Pin
josepaulino12-Jun-07 4:19
josepaulino12-Jun-07 4:19 
GeneralRe: Word user confirmations Pin
Nicolas MEURET14-Jun-07 6:08
Nicolas MEURET14-Jun-07 6:08 
Don't forget that WinWord is not a simple 'InProcess' ActiveX, but only an application that provide a OLE interface to automate it.

The problem is that even if you work with Word in automation and it is not visible, it doesn't mean that it won't display any blocking question dialog !!
AnswerRe: Word user confirmations Pin
josepaulino14-Jun-07 7:05
josepaulino14-Jun-07 7:05 
GeneralRe: Word user confirmations Pin
Beatrice Monk22-Oct-14 23:40
professionalBeatrice Monk22-Oct-14 23:40 

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

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