Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / May 2006

Tip: Looking for answers? Try searching our database.

generating Reports using VB.net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
z - 26 Apr 2006 07:39 GMT
hi i want to ask how can we retrieve data from MS sql server table
northwind and generate report of same data in formated form in MS word
using VB.net ... i need its code can any one help please .. its real
urgent .. i m currently using Visual Studio 2003 ... and MS SQL server
2003 thanks in advance...
Cindy M  -WordMVP- - 02 May 2006 17:20 GMT
Hi Z,

> i want to ask how can we retrieve data from MS sql server table
> northwind and generate report of same data in formated form in MS word
> using VB.net ... i need its code can any one help please .. its real
> urgent .. i m currently using Visual Studio 2003 ... and MS SQL server
> 2003 thanks in advance...

It's certainly possible, and not even too difficult... if you have the
proper background knowledge. If you haven't, then you're going to need
to dig in, or hire someone with the requisite knowledge.

Basically, you drop the information into bookmarks on the prepared Word
document.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :-)
z - 03 May 2006 11:32 GMT
Imports Word.ApplicationClass
Imports Excel.ApplicationClass
Imports Microsoft.Office.Core
'Imports Microsoft.Office.Interop
Imports System.IO.Path
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Public Class Form1
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
       MyBase.New()

       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form
Designer
   'It can be modified using the Windows Form Designer.
   'Do not modify it using the code editor.
   Friend WithEvents Button1 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button
       Me.SuspendLayout()
       '
       'Button1
       '
       Me.Button1.Location = New System.Drawing.Point(24, 24)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(120, 80)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Automate Word"
       '
       'Form1
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(168, 133)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)

   End Sub

#End Region
   Private WithEvents oWord As Word.ApplicationClass
   Private WithEvents ThisApplication As Word.Application
   Private WithEvents ThisDocument As Word.Application
   Private WithEvents WordApp As Word.ApplicationClass
   Private WithEvents openFileDialog1 As OpenFileDialog
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
       CreateWordTable()
   End Sub

   Private Sub CreateWordTable()
       oWord = CreateObject("Word.Application")
       oWord.Visible = False

       oWord.Documents.Add()
       Dim rng As Word.Range = oWord.ActiveDocument.Range(Start:=0,
End:=0)
       rng.InsertBefore("Author's Contact")
       rng.Font.Name = "Verdana"
       rng.Font.Size = 16
       rng.InsertParagraphAfter()
       rng.InsertParagraphAfter()
       rng.SetRange(rng.End, rng.End)
       rng.Tables.Add( _
           Range:=oWord.ActiveDocument.Paragraphs(2).Range, _
           NumRows:=1, NumColumns:=3)
       Dim tbl As Word.Table = oWord.ActiveDocument.Tables(1)
       tbl.Range.Font.Size = 12
       tbl.Range.Font.Name = "Verdana"
       tbl.Borders.InsideLineStyle = _
           Word.WdLineStyle.wdLineStyleSingle
       tbl.Borders.OutsideLineStyle = _
           Word.WdLineStyle.wdLineStyleDouble
       tbl.Columns(1).SetWidth( _
          oWord.Application.InchesToPoints(1.5), _
       Word.WdRulerStyle.wdAdjustNone)
       tbl.Columns(2).SetWidth( _
   oWord.Application.InchesToPoints(2.25), _
       Word.WdRulerStyle.wdAdjustNone)
       tbl.Columns(3).SetWidth( _
          oWord.Application.InchesToPoints(3.25), _
           Word.WdRulerStyle.wdAdjustNone)
       tbl.Cell(1, 1).Range.Text = "Author"
       tbl.Cell(1, 2).Range.Text = "Contact#"
       Dim rngCell As Word.Range = tbl.Cell(1, 3).Range
       rngCell.Text = "Address"
       rngCell.ParagraphFormat.Alignment = _
           Word.WdParagraphAlignment.wdAlignParagraphRight
       Dim strSQL As String = _
           "SELECT au_lname, phone, address " & _
           "FROM authors"

       Dim cnn As SqlConnection
       Dim sdr As SqlDataReader
       Dim cmd As SqlCommand
       cnn = New SqlConnection( _
           "workstation id='DEV-INTERN';packet size=4096;integrated
security=SSPI;data source='DEV-INTERN';persist security
info=False;initial catalog=pubs")
       cnn.Open()
       cmd = New SqlCommand(strSQL, cnn)
       sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
       Dim intRow As Integer = 2
       While sdr.Read()
           tbl.Rows.Add()
           tbl.Cell(intRow, 1).Range.Text = sdr(0).ToString
           tbl.Cell(intRow, 2).Range.Text = sdr(1).ToString
           tbl.Cell(intRow, 3).Range.Text = sdr(2).ToString
           intRow += 1
       End While
       tbl.Rows(1).Range.Bold = 1

       oWord.ActiveDocument.SaveAs("C:\Documents and
Settings\zafzal\Desktop\word.doc")
       oWord.ActiveDocument.Close()
       Close()

   End Sub

   
End Class

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.