Tag Archives: Office Programming

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

Example to create an e-mail message in Outlook.

 
This example shows how to create an e-mail message in Outlook.

Private Sub CreateMailItem()
      Dim objItem As Outlook.MailItem = _
            Me.CreateItem(Outlook.OlItemType.olMailItem)
      objItem.Subject = "Hello Author"
      objItem.To = "aboutus@authorcode.com"
      objItem.Body = "Hello Author, i want to contact you"
      objItem.Importance = Outlook.OlImportance.olImportanceLow
      objItem.Display(False)
End Sub

How to find Count of total Characters in word document programmatically

 
If you want to get count of total characters in any word document at run time in .net, you can use following code samples.

Code Sample :

[vb.net]
vb.net code for finding count of characters in word document.

Private Function GetCharectorsCount() As Integer
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        objWord = CreateObject("Word.Application")
        objDoc = objWord.Documents.Open("D:\Test.docx")
        Dim TotalCount As Integer = objDoc.Characters.Count
        Return TotalCount
    End Function

[c#]
C# code for finding count of characters in word document.

    private static int  GetWordCharacters()
        {
            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);
            int totalCount = objDoc.Characters.Count;
            return totalCount;
        }

Note:

All characters in the document are counted, including spaces, paragraph marks, and other characters that are normally hidden. Even a new, blank document returns a count of one character because it contains a paragraph mark.

How to open existing word document in vb.net

 

This example shows how to open an existing word document in vb.net.
Add reference of ‘Microsoft.Office.Interop.Word’ in your project.
This code example requires a document named Test.docx must exist in a D: drive.

Code Sample :

Private Sub OpenWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        objWord = CreateObject("Word.Application")
        objDoc = objWord.Documents.Open("D:\Test.docx")
        objWord.Visible = True
End Sub

Create a new Word document in c#

 
This article describes how to create a new Word document in c#.

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. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

 private void CreateWordDocument()
        {
            object oMissing = System.Reflection.Missing.Value;
 
            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);
 
            Microsoft.Office.Interop.Word.Paragraph objPara;
            objPara = objDoc.Content.Paragraphs.Add(ref oMissing);
            objPara.Range.Text = "Welcome to Author Code";
            objPara.Range.Font.Bold = 1;
            objPara.Format.SpaceAfter = 30;   
            objPara.Range.InsertParagraphAfter();
        }

Create a new Word document using VB.Net

 
This article describes how to create a new Word document using VB.Net.
follow these steps:
1. Start a new Windows application in vb.net language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

 Private Sub CreateWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
 
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
        'Insert some text in document
        Dim objPara As Word.Paragraph
        objPara = objDoc.Content.Paragraphs.Add
        objPara.Range.Text = "Welcome to Author Code"
        objPara.Range.Font.Bold = True
        objPara.Format.SpaceAfter = 30
        objPara.Range.InsertParagraphAfter()
 
    End Sub

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

Create a new Word document and insert a table using VB.Net

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

follow these steps:
1. Start a new Windows application in vb.net language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

Private Sub Button1_Click(ByVal sender As System.Object,
                              ByVal e As System.EventArgs) Handles Button1.Click
        CreateTableInWordDocument()
End Sub
Private Sub CreateTableInWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim objTable As Word.Table
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
        Dim r As Integer, c As Integer
 
        objTable = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
        objTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
            For c = 1 To 5
                objTable.Cell(r, c).Range.Text = "Row" & r & " Coulmn" & c
            Next
        Next
        objTable.Rows.Item(1).Range.Font.Bold = True
        objTable.Rows.Item(1).Range.Font.Italic = True
        objTable.Borders.Shadow = True
        Me.Close()
    End Sub