word interop table

bnck

New member
Joined
May 21, 2020
Messages
1
Programming Experience
Beginner
I am newbee for vb.net. I managed to develop a desktop application that creates word document from DataGridView control. it does work well. I use VS2019, MySQL server, Office 2013. word document created dynamically has few paragraphs and a table. My issue is app does create Word Document for one time. next time it does generate only paragraph but NO TABLE.
I have to restart VS and it does create only one time. here is code segment that generate table. Please give an advice. Thanks

VB.NET:
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, dgvData.Rows.Count, dgvData.ColumnCount)
oTable.Borders.OutsideColor = Word1.WdColor.wdColorBlack
oTable.Borders.OutsideLineStyle = Word1.WdLineStyle.wdLineStyleSingle
oTable.Borders.InsideColor = Word1.WdColor.wdColorBlack
oTable.Borders.InsideLineStyle = Word1.WdLineStyle.wdLineStyleSingle
oTable.Range.ParagraphFormat.SpaceAfter = 6

oTable.Rows.Add()
oTable.Cell(1, 1).Range.Text = "Sr.No"
oTable.Cell(1, 2).Range.Text = "CP No"
oTable.Cell(1, 3).Range.Text = "Ministry"
oTable.Cell(1, 4).Range.Text = "Title & Date"

Dim actRow As Integer = 2

For row As Integer = 0 To dgvData.Rows.Count - 1
    oTable.Rows.Add()
    
    For cell As Integer = 0 To dgvData.ColumnCount - 1
        oTable.Cell(actRow, cell + 1).Range.Text = dgvData.Rows(row).Cells(cell).Value.ToString()
    Next
    
    actRow = actRow + 1
Next

oTable.Rows.Item(1).Range.Font.Bold = 1
oTable.Columns(1).Width = oWord.InchesToPoints(0.5)
oTable.Columns(2).Width = oWord.InchesToPoints(1.5)
oTable.Columns(3).Width = oWord.InchesToPoints(2)
oTable.Columns(4).Width = oWord.InchesToPoints(3)
'oTable.Columns(4).
'oTable.Rows.Item(1).Range.Font.Italic = 1

'Add some text after the table.
oTable.Range.InsertParagraphAfter()
oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara4.Range.InsertParagraphBefore()
oPara4.Range.Text = "And here's goes paragraph again:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagraphAfter()

.................................................... rest code goes ......
 
Last edited by a moderator:
Back
Top