CodeSnip: Generating Word Reports in WinForms Using Microsoft Office Library
 
Published: 31 Jan 2007
Abstract
In this article I will demonstrate how to use Visual Basic 2005 and the Microsoft Office object library to generate custom Word reports from a Win forms application.
by Vishal Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34011/ 89

Introduction

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 = "normal.dot" ' 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
  collection
  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
  aDoc.Activate()
  'Add Header
  wordApplication.Selection.Font.Size = 14
  wordApplication.Selection.Font.Bold = True
  wordApplication.Selection.Font.Underline =
  Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineThick
  wordApplication.Selection.TypeParagraph()
  wordApplication.Selection.ParagraphFormat.Alignment =
  Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
  wordApplication.Selection.TypeText(Header)
  'Add Report Date
  wordApplication.Selection.Font.Size = 12
  wordApplication.Selection.Font.Bold = False
  wordApplication.Selection.Font.Underline =
  Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone
  wordApplication.Selection.ParagraphFormat.Alignment =
  Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight
  wordApplication.Selection.TypeText("Date: " & Now.ToShortDateString &
  vbNewLine & vbNewLine)
  'Add Content
  wordApplication.Selection.Font.Size = 12
  wordApplication.Selection.Font.Bold = False
  wordApplication.Selection.Font.Underline =
  Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone
  wordApplication.Selection.ParagraphFormat.Alignment =
  Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft
  wordApplication.Selection.TypeText("Following Employees data has been
  deleted
   " & 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

Downloads

References

Conclusion

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.



User Comments

Title: asdfsdf   
Name: dsfdasf
Date: 2012-05-03 8:28:58 AM
Comment:
adsfadsfasdf
Title: How to save this file   
Name: Nick
Date: 2010-06-14 11:25:07 PM
Comment:
Hello Sir,
Can you please guide me for How to save this file instead of opening directly?
Title: Thanking you   
Name: madhu kumar
Date: 2009-03-25 8:37:22 AM
Comment:
Hi Vishal
Your article is really helpful. I am just starting with implementation of Microsoft.Office.Interop.Word namespace samples. Thank you for this.

I wish you provide more samples.

Regards
Madhu Kumar.B
Title: M   
Name: Naveed
Date: 2007-09-19 10:16:19 AM
Comment:
'FOR PAGE BREAK
SELECTION.TYPETEXT(CHR(12))
'This will add the Page break to the word document
Title: KoOl Code   
Name: Naveed
Date: 2007-09-18 4:09:00 AM
Comment:
kOol Article.. I was looking for a good a code and i find yours.
Thanksss
Title: Output to WORDS in ASP.NET   
Name: Lennie Kuah
Date: 2007-08-20 7:14:10 PM
Comment:
I am using DataReader and looping thru it. On each loop I am trying to output the data to WORDS document. When there is a change in ProductID, I want to insert a page break. How do I do that, please help.

Thanks
Title: word report in visual studio 2005   
Name: kirti
Date: 2007-05-24 6:55:08 AM
Comment:
how can generate word report in visual studio 2005
Title: Image   
Name: Praveen
Date: 2007-05-10 12:58:21 PM
Comment:
How can I add an image from a database field to this file?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-01 8:28:32 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search