none
Using a Word template with VB

    Question

  • Hi all,

    I'm doing a project in VB. I have some textboxes in my VB user interface. Whenever the user enter value in those boxes and submit, i wan't to generate a Microsoft Word document with the values entered, incorporated with a template previously defined.

    A standard template for the word is present. Only some fields in the template needs to be changed every time. These values will be supplied by the user through the interface. Finally when all the changes are made, the word document should be made with all those values.

    Could anybody provide me a solution for this?

    Thanks in advance

    Renjith
    Thursday, July 10, 2008 10:32 AM

Answers

  • Hi Renjith,

    The following code sample demonstrates how to open existing word template, 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.

    Imports Word = Microsoft.Office.Interop.Word  
     
    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            ' Create Word Application     
            Dim oWord As Word.Application = CreateObject("Word.Application")  
     
            ' Open existing word template     
            Dim oDoc As Word.Document = oWord.Documents.Open("C:\template.dot")  
            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 template     
            oDoc.Save()  
     
            oDoc.Close()  
            oDoc = Nothing 
            oWord.Application.Quit()  
            oWord = Nothing 
        End Sub 
     
    End Class 

    Trackback: http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/6b9bdf3e-aea7-4eb3-a518-56e5108fa202/ 

    The followng table content will be inserted this Word template file:

    Row1Col1

    Row1Col2

    Row1Col3

    Row1Col4

    Row1Col5

    Row2Col1

    Row2Col2

    Row2Col3

    Row2Col4

    Row2Col5

    Row3Col1

    Row3Col2

    Row3Col3

    Row3Col4

    Row3Col5

      
    Some references:

    How to Use Automation to Find a Location in a Word Document
    http://support.microsoft.com/kb/154568

    Most common tasks are performed in Word Automation
    http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

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

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

    How to insert table and fill it with specific data using Word Automation
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3004154&SiteID=1


    Best regards,
    Martin Xie

    Thursday, July 17, 2008 4:41 AM