Skip to content
 

Batch printing Word documents

I recently worked on a project where one of the tasks needing to be done was printing previously-created Word documents in batch, from a Windows fat-client program. The specification required that the user not see the Word doc. The class below provides a PrintDocument() method that automates Word to open the document and print it, without making Word document visible.

Windows only!

Do note that this code is only for use in a fat client environment. Word automation won’t work in multi-threaded, service-side environments such as ASP.NET. Don’t attempt to use this code in a Web app. Although the code presented here doesn’t show the Word document, Word does indeed get loaded (albeit invisibly) to print the document.

My general language of choice is C#, but I’ve learned that its much easier to code Office automation tasks with VB.NET than it is any other .NET language. My friends from England would call this “Horses for Courses.” The upcoming C# 4.0 enhancements may (hopefully!) change this, but until then, I’m going to stick with using VB.NET for Office automation.

Figure 1. A helper class to print a Word document
  1Imports System.IO 
  2Imports System.Windows.Forms 
  3Imports System.Drawing
  4
  5Namespace rp.OfficeHelpers
  6
  7    Public Enum PrintStatus
  8        Success
  9        FileNotFound
 10        FailedToOpenDocument
 11        FailedToPrintDocument
 12    End Enum
 13
 14    Public Class Word
 15
 16        Public Shared Function PrintDocument( DocumentName As String, _  
 17                                              PrinterName As String ) As PrintStatus 
 18
 19            PrintDocument( DocumentName, PrinterName, 1 ) 
 20        End Function
 21
 22        Public Shared Function PrintDocument( DocumentName As String, _  
 23                                              PrinterName As String, _ 
 24                                              NumberOfCopies As Integer ) As PrintStatus 
 25
 26            Dim wordApp As Microsoft.Office.Interop.Word.Application = _ 
 27                           new Microsoft.Office.Interop.Word.Application()
 28            Dim wordDoc As Microsoft.Office.Interop.Word.Document
 29            Dim CurrentPrinter As String = wordApp.ActivePrinter
 30
 31            If ( Not File.Exists( DocumentName ) )
 32                Return PrintStatus.FileNotFound    
 33            End If
 34
 35            wordApp.Visible = false
 36         
 37            wordApp.ActivePrinter = PrinterName
 38
 39            ' Document name must be provided as an object, not a string.
 40            Try 
 41                wordDoc = wordApp.Documents.Open( CType( DocumentName, Object ) )
 42            Catch WordError as System.Exception 
 43                Return PrintStatus.FailedToOpenDocument
 44            End Try  
 45         
 46            Try 
 47                wordDoc.PrintOut( Copies := CType( NumberOfCopies, Object ), Background:= false )
 48            Catch WordError as System.Exception 
 49                Return PrintStatus.FailedToPrintDocument
 50            End Try  
 51                                                                  
 52            wordApp.ActivePrinter = CurrentPrinter
 53
 54            wordApp.Quit( SaveChanges := false )
 55
 56            Return PrintStatus.Success        
 57        End Function
 58
 59    End Class
 60
 61End Namespace

Leave a Reply