none
data from form to document RRS feed

  • Question

  • I am new to programming and I am trying to take a VB 2008 form , pull data from a sql 2005 express database then insert it into a windows document.  I have built the form and can get the data I just need to get it to the word document

    Thanks for any help
    If it would help I can supply more information.
    Jlanham
    Wednesday, March 12, 2008 4:26 PM

Answers

  •  Jlanham wrote:
    I pull data from a sql 2005 express database then insert it into a windows document.  I have built the form and can get the data I just need to get it to the word document.

     

    Hi Jlanham,

     

    Welcome to MSDN forums!

     

    The following code sample demonstrates how to new a word document, insert a 3 x 5 table and fill it with specific data, finally save it.

     

    You need to Add Reference COM component: Microsoft Word Object Library to your project.

    Code Snippet

    Imports Word = Microsoft.Office.Interop.Word

     

    Public Class Form2

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

            ' Create Word Application

            Dim oWord As Word.Application = CreateObject("Word.Application")

            ' Create new word document

            Dim oDoc As Word.Document = oWord.Documents.Add()

            Dim oTable As Word.Table

            oWord.Visible = True

     

            'Insert a 3 x 5 table and fill it with specific data

            Dim r As Integer, c As Integer

            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)

            oTable.Range.ParagraphFormat.SpaceAfter = 6

            For r = 1 To 3

                For c = 1 To 5

                    oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c

                Next

            Next

            'make the first row bold and italic

            oTable.Rows.Item(1).Range.Font.Bold = True

            oTable.Rows.Item(1).Range.Font.Italic = True

     

            ' Save this word document

            oDoc.SaveAs("C:\1.doc", True)

     

            oDoc.Close()

            oDoc = Nothing

            oWord.Application.Quit()

            oWord = Nothing

     

        End Sub

     

    End Class

     

    The output in 1.doc is as below:

    Row1Col1

    Row1Col2

    Row1Col3

    Row1Col4

    Row1Col5

    Row2Col1

    Row2Col2

    Row2Col3

    Row2Col4

    Row2Col5

    Row3Col1

    Row3Col2

    Row3Col3

    Row3Col4

    Row3Col5

     

    Detail code sample inside this thread:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2547110&SiteID=1

     

    How to automate Word from Visual Basic .NET to create a new document

    http://support.microsoft.com/kb/316383/

     

     

    Regards,

    Martin

    Friday, March 14, 2008 4:39 AM

All replies

  •  Jlanham wrote:
    I pull data from a sql 2005 express database then insert it into a windows document.  I have built the form and can get the data I just need to get it to the word document.

     

    Hi Jlanham,

     

    Welcome to MSDN forums!

     

    The following code sample demonstrates how to new a word document, insert a 3 x 5 table and fill it with specific data, finally save it.

     

    You need to Add Reference COM component: Microsoft Word Object Library to your project.

    Code Snippet

    Imports Word = Microsoft.Office.Interop.Word

     

    Public Class Form2

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

            ' Create Word Application

            Dim oWord As Word.Application = CreateObject("Word.Application")

            ' Create new word document

            Dim oDoc As Word.Document = oWord.Documents.Add()

            Dim oTable As Word.Table

            oWord.Visible = True

     

            'Insert a 3 x 5 table and fill it with specific data

            Dim r As Integer, c As Integer

            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)

            oTable.Range.ParagraphFormat.SpaceAfter = 6

            For r = 1 To 3

                For c = 1 To 5

                    oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c

                Next

            Next

            'make the first row bold and italic

            oTable.Rows.Item(1).Range.Font.Bold = True

            oTable.Rows.Item(1).Range.Font.Italic = True

     

            ' Save this word document

            oDoc.SaveAs("C:\1.doc", True)

     

            oDoc.Close()

            oDoc = Nothing

            oWord.Application.Quit()

            oWord = Nothing

     

        End Sub

     

    End Class

     

    The output in 1.doc is as below:

    Row1Col1

    Row1Col2

    Row1Col3

    Row1Col4

    Row1Col5

    Row2Col1

    Row2Col2

    Row2Col3

    Row2Col4

    Row2Col5

    Row3Col1

    Row3Col2

    Row3Col3

    Row3Col4

    Row3Col5

     

    Detail code sample inside this thread:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2547110&SiteID=1

     

    How to automate Word from Visual Basic .NET to create a new document

    http://support.microsoft.com/kb/316383/

     

     

    Regards,

    Martin

    Friday, March 14, 2008 4:39 AM
  • Thanks Martin this helps me pull up a word doc but I still need help.  I see I should have given a bit more information in my first post.


    I have a sql database that has multiple fields that I pull data from.  On my visual basic form I have combo boxes that are  linked to the database. Selecting the data I need, I push a button and I need the application to open a word document that is already prepared, pull the
    data from the data base and insert the data into specific fields in the document then print it.  I don't need to save the document just close it.
    I have the form and I can select the data and I can open the form now. But how to write the data to the specific location on the word document is the trick I am having problems with at this time.

    All help is appreciated

    Jlanham
    Thursday, April 24, 2008 1:29 PM
  •  Jlanham wrote:

    But how to write the data to the specific location on the word document is the trick I am having problems with at this time.

     

    Please check the following KB articles:

     

    HOW TO: Use Automation to Find a specific Location in a Word Document

    http://support.microsoft.com/kb/210032

     

    How to automate Word from Visual Basic .NET to create a new document
    http://support.microsoft.com/kb/316383/

    Friday, April 25, 2008 3:20 AM