Create a new Word document and insert a table using VB.Net

This article describes how to create a new Word document and insert a table using VB.Net, it also shows how to format tables, and populate the tables with data.

follow these steps:
1. Start a new Windows application in language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

Private Sub Button1_Click(ByVal sender As System.Object,
                              ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub CreateTableInWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim objTable As Word.Table
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
        Dim r As Integer, c As Integer
        objTable = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
        objTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
            For c = 1 To 5
                objTable.Cell(r, c).Range.Text = "Row" & r & " Coulmn" & c
        objTable.Rows.Item(1).Range.Font.Bold = True
        objTable.Rows.Item(1).Range.Font.Italic = True
        objTable.Borders.Shadow = True
    End Sub

2 thoughts on “Create a new Word document and insert a table using VB.Net”

Comments are closed.