How do I reference Column header from a datagridview from VB to MS Word?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have successfully exported the contents of a datagrid. But the column header of the data grid is not visible in MS word. How do I code this? Heres my code: Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim otable As Word.Table
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add("Word template")
oDoc.Bookmarks("W_SupplierName").Range.Text = Company_NameLabel1.Text
oDoc.Bookmarks("W_ContactNo").Range.Text = Contact_PersonLabel1.Text
oDoc.Bookmarks("W_ContactPerson").Range.Text = Contact_PersonLabel1.Text
oDoc.Bookmarks("W_POnum").Range.Text = txtsearch.Text
oDoc.Bookmarks("W_OrderDate").Range.Text = Order_DateLabel1.Text
oDoc.Bookmarks("W_DeliveryDate").Range.Text = Delivery_DateLabel1.Text
oDoc.Bookmarks("approvedtxt").Range.Text = Approved_byLabel1.Text
oDoc.Bookmarks("Preptxt").Range.Text = Prepared_byLabel1.Text
Dim r As Integer, c As Integer
otable = oDoc.Tables.Add(oDoc.Bookmarks.Item("table").Range, DataGridView.RowCount,DataGridView.ColumnCount)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 0 To DataGridView.RowCount - 1
For c = 0 To DataGridView.ColumnCount - 1
otable.Cell(r + 1, c + 1).Range.Text =DataGridView(c, r).Value.ToString()
Next
Next
otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
otable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle

View the full article
 
Back
Top