Tag Archives: word programming in c#

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);
        }

How to print current page of active word document in .net

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

Code Sample :

[vb.net]

Private Sub PrintCurrentpageInWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        objWord = CreateObject("Word.Application")
        objDoc = objWord.Documents.Open("D:\Test.docx")
        objDoc.PrintOut(Background:=True, Append:=False, _
                        Range:=Word.WdPrintOutRange.wdPrintCurrentPage, _
                       Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
                       Copies:="1", Pages:="1", PageType:=Word.WdPrintOutPages.wdPrintAllPages, _
                       PrintToFile:=False, Collate:=True, ManualDuplexPrint:=False)
        objDoc.Close()
        objDoc = Nothing
        objWord.Quit()
 End Sub

[c#]

 private void PrintCurrentpageInWordDocument()
        {
            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 = "1";
            object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintCurrentPage;
            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);
        }

Create a new Word document and insert a table in c#

 
This article describes how to create a new Word document and insert a table using c#, it also shows how to format tables, and populate the tables with data.

follow these steps:
1. Start a new Windows application in C# language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Create a Button1 click event.
5. Put this code on the click event of button1:

 private void button1_Click(object sender, EventArgs e)
        {
            CreateTableInWordDocument();
        }
 private void CreateTableInWordDocument()
        {
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; 
            Microsoft.Office.Interop.Word._Application objWord;
            Microsoft.Office.Interop.Word._Document objDoc;
            objWord = new Microsoft.Office.Interop.Word.Application();
            objWord.Visible = true;
            objDoc = objWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
 
            int i = 0;
            int j = 0;
            Microsoft.Office.Interop.Word.Table objTable;
            Microsoft.Office.Interop.Word.Range wrdRng = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            objTable = objDoc.Tables.Add(wrdRng, 3, 4, ref oMissing, ref oMissing);
            objTable.Range.ParagraphFormat.SpaceAfter = 7;
 
            string strText;
            for (i = 1; i <= 3; i++)
                for (j = 1; j <= 4; j++)
                {
                    strText = "Row" + i + " Coulmn" + j;
                    objTable.Cell(i, j).Range.Text = strText;
                }
            objTable.Rows[1].Range.Font.Bold = 1;
            objTable.Rows[1].Range.Font.Italic = 1;
            objTable.Borders.Shadow = true;
            this.Close();
        }