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

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.

View Entire Article

User Comments

Title: asdfsdf   
Name: dsfdasf
Date: 2012-05-03 8:28:58 AM
Title: How to save this file   
Name: Nick
Date: 2010-06-14 11:25:07 PM
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
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.

Madhu Kumar.B
Title: M   
Name: Naveed
Date: 2007-09-19 10:16:19 AM
'This will add the Page break to the word document
Title: KoOl Code   
Name: Naveed
Date: 2007-09-18 4:09:00 AM
kOol Article.. I was looking for a good a code and i find yours.
Title: Output to WORDS in ASP.NET   
Name: Lennie Kuah
Date: 2007-08-20 7:14:10 PM
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.

Title: word report in visual studio 2005   
Name: kirti
Date: 2007-05-24 6:55:08 AM
how can generate word report in visual studio 2005
Title: Image   
Name: Praveen
Date: 2007-05-10 12:58:21 PM
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-2024  |  Page Processed at 2024-07-13 12:45:48 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search