CodeSnip: Generating Word Reports in WinForms Using Microsoft Office Library
page 2 of 6
by Vishal Kumar Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 16543/ 188

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.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 3 and type the answer here:

User Comments

Title: M   
Name: Naveed
Date: 9/19/2007 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: 9/18/2007 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: 8/20/2007 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: 5/24/2007 6:55:08 AM
Comment:
how can generate word report in visual studio 2005
Title: Image   
Name: Praveen
Date: 5/10/2007 12:58:21 PM
Comment:
How can I add an image from a database field to this file?

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






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


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/8/2009 8:36:25 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search