Print word document programmatically in .net

 
We can use can use the PrintOut method to print a Microsoft Office Word document, following example code shows how to print whole word document in vb.net and c# languages:

Code Sample :

[vb.net]

    Private Sub PrintWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        objWord = CreateObject("Word.Application")
        objDoc = objWord.Documents.Open("D:\Test.docx")
        objDoc.PrintOut()
        objDoc.Close()
        objDoc = Nothing
        objWord.Quit()
    End Sub

[c#]

 private void PrintWordDocument()
        {
            object objMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application objWord;
            Microsoft.Office.Interop.Word.Document objDoc;
            objWord = new Microsoft.Office.Interop.Word.Application();
            object fileName = @"D:\Test.docx";
            objDoc=objWord.Documents.Open(ref fileName,
                ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
 
            object copies = "1";
            object pages = "";
            object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
            object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
            object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
            object objTrue = true;
            object objFalse = false;
 
            objDoc.PrintOut(
                ref objTrue, ref objFalse, ref range, ref objMissing, ref objMissing, ref objMissing,
                ref items, ref copies, ref pages, ref pageType, ref objFalse, ref objTrue,
                ref objMissing, ref objFalse, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
        }

2 thoughts on “Print word document programmatically in .net”

Comments are closed.