SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size

Export Data from DataGridView in Visual Basic 2008 to Microsoft Word Table

E-mail Print PDF

This article shows you how to write code in Visual Basic 2008 to export data from DataGridView control into Microsoft Word's Table.


Dim objWordApp As Object
Dim objWordDoc As Object

objWordApp = CreateObject("Word.Application")
objWordDoc = objWordApp.documents.add()

objWordDoc.tables.add(objWordDoc.range(0, 0), 11, 3)
With objWordDoc.tables(1)
     .borders.InsideLineStyle = 1
     .borders.OutsideLineStyle = 1

     For i As Integer = 0 To 10
          .rows(i + 1).cells(1).range.text = Me.dgvData.Rows(i).Cells("id").Value
          .rows(i + 1).cells(2).range.text = Me.dgvData.Rows(i).Cells("name").Value
          .rows(i + 1).cells(3).range.text = Me.dgvData.Rows(i).Cells("gender").Value
     Next
End With

objWordApp.visible = True

objWordApp = Nothing

Download Sample Code Here