LogoASPAlliance: Articles, reviews, and samples for .NET Developers
CodeSnip: Generating Word Reports in WinForms Using Microsoft Office Library
by Vishal Patil
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34001/ 84


Organizations often create applications that serve both internal and external customers. For example, an organization might need to regularly generate customized Word documents for both users and customers. Fortunately, you can use COM objects with Microsoft Office applications. Office lets you drive most of its features from a programming language such as Visual Basic 2005. This compatibility lets you automate almost any task.

In the below example I show you how to use Visual Basic 2005 and Word to create custom reports in Win forms.

Creating the User Interface

Create a Windows Application project with the name WordReportsinWinForms. To this, add a form and button which should look like the one below.

Figure 1

Add a Reference to Microsoft Word 11.0 object Library as shown below.

Figure 2


In the Generate Report button (Figure 1) click event and add the following code.

Listing 1

Private Sub btnGenerateReport_Click(ByVal sender As System.ObjectByVal e As
  System.EventArgs) Handles btnGenerateReport.Click
  Dim Header As String = "Employee Administration: Audit Report" & vbNewLine
  & vbNewLine
  Dim wordApplication As Microsoft.Office.Interop.Word.ApplicationClass
  wordApplication = New Microsoft.Office.Interop.Word.ApplicationClass
  'Create New Document in Word
  Dim missing As Object = System.Reflection.Missing.Value
  Dim fileName As Object = "" ' template file name
  Dim newTemplate As Object = False
  Dim docType As Object = 0
  Dim isVisible As Object = True
  ' Create a new Document, by calling the Add function in the Documents
  Dim aDoc As Microsoft.Office.Interop.Word.Document =
  wordApplication.Documents.Add(fileName, newTemplate, docType, isVisible)
  ' need to see the created document, so make it visible
  wordApplication.Visible = True
  'Add Header
  wordApplication.Selection.Font.Size = 14
  wordApplication.Selection.Font.Bold = True
  wordApplication.Selection.Font.Underline =
  wordApplication.Selection.ParagraphFormat.Alignment =
  'Add Report Date
  wordApplication.Selection.Font.Size = 12
  wordApplication.Selection.Font.Bold = False
  wordApplication.Selection.Font.Underline =
  wordApplication.Selection.ParagraphFormat.Alignment =
  wordApplication.Selection.TypeText("Date: " & Now.ToShortDateString &
  vbNewLine & vbNewLine)
  'Add Content
  wordApplication.Selection.Font.Size = 12
  wordApplication.Selection.Font.Bold = False
  wordApplication.Selection.Font.Underline =
  wordApplication.Selection.ParagraphFormat.Alignment =
  wordApplication.Selection.TypeText("Following Employees data has been
   " & vbNewLine & vbNewLine)
  wordApplication.Selection.TypeText("1) Domnic   Suez" & vbNewLine)
  wordApplication.Selection.TypeText("2) Suzaana Cork" & vbNewLine)
  wordApplication.Selection.TypeText("3) Al Donald" & vbNewLine)
  wordApplication.Selection.TypeText("4) Andrew Peter" & vbNewLine)
  wordApplication.Selection.TypeText("5) Alan John" & vbNewLine)
End Sub

In the above listing the Word application is instantiated and a new document is created. Then the Report Header, Date and content are added to the document using the Word Application properties such as Fonts, Underlines and alignments.

Generating the Report

Now, run the application and click the Generate Report button (Figure 1) which generates Report below.

Figure 3


Customizable reports can be generated in Win forms application using Microsoft Word.

This is an easy and convenient way for the creation and customization of reports and takes advantage of the formatting and presentation capabilities of Microsoft Word.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-07 7:11:53 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search