none
DGV cells to Word.Document table cells via bookmark adding ? RRS feed

  • Question

  • Greetings,

    I have a DGV with 7 columns and some rows (not always a fixed number of rows) , I also have a word template .docx which also contains a table with 7 columns and one single empty row. I set a bookmark point to each cell of the table in word file accordingly (7 bookmarks on the whole). I use the code below to add value of onc cell in DGV to a bookmark point in word document :

     Dim Fword As Word.Application
            Dim Fdoc As Word.Document
            Fword = CreateObject("Word.Application")
            Fword.Visible = False
            Fword.Application.DisplayAlerts = False
    
    
    
    
            Fdoc = Fword.Documents.Add(Application.StartupPath & "\MyTemplate.docx")
    
    
    
            Fdoc.Bookmarks.Item("P1").Range.Text = DataGridView1.Rows(0).Cells(0).Value.ToString

    If there is only one row in DGV , that would be fine to add the row to table in word , because my table has one empty row. But what if there are numerous rows ? I want word to add rows to its table in accordance with DGV rows.

    I would be thankful if you help me out.





    • Edited by Kevin993 Wednesday, September 26, 2018 5:05 PM
    Wednesday, September 26, 2018 5:03 PM

Answers

  • Hi,

    I have not reported an error here.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kevin993 Sunday, September 30, 2018 10:12 PM
    Thursday, September 27, 2018 9:35 AM

All replies

  • Hi,

    Is it possible to build a table in the word file?

    code:

    Imports Microsoft.Office.Interop
    Imports System.Data.SqlClient
    
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                dt = New System.Data.DataTable()
                sda.Fill(dt)
                DataGridView1.DataSource = dt
            End Using
        End Sub
    
        Public Sub ExportDataGridView(ByVal srcDgv As DataGridView)
    
            Dim path As Object = "D:\test.docx"
            Dim none As Object = System.Reflection.Missing.Value
    
            Dim wordApp As Word.Application = New Word.Application()
    
    
            Dim document As Word.Document = wordApp.Documents.Add(none, none, none, none)
            Dim table As Word.Table = document.Tables.Add(document.Paragraphs.Last.Range, DataGridView1.RowCount, DataGridView1.ColumnCount)
            With table
    
                .Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle
                .Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle
            End With
            Try
                For i As Integer = 0 To srcDgv.Columns.Count - 1
                    table.Cell(0, i + 1).Range.Text = srcDgv.Columns(i).HeaderText
                Next
    
                For i As Integer = 1 To srcDgv.Rows.Count - 1
    
                    For j As Integer = 0 To srcDgv.Columns.Count - 1
                        table.Cell(i + 1, j + 1).Range.Text = srcDgv(j, i - 1).Value.ToString()
                    Next
                Next
                document.SaveAs(path, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none)
                document.Close(none, none, none)
                MessageBox.Show("success")
            Finally
                wordApp.Quit(none, none, none)
            End Try
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ExportDataGridView(DataGridView1)
    
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 27, 2018 2:38 AM
  • Hi,

    Is it possible to build a table in the word file?

    code:

    Imports Microsoft.Office.Interop Imports System.Data.SqlClient Public Class Form1 Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf" Dim conn As SqlConnection Dim sda As SqlDataAdapter Dim dt As DataTable Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Using conn = New SqlConnection(constr) conn.Open() sda = New SqlDataAdapter("Select * From Student", conn) dt = New System.Data.DataTable() sda.Fill(dt) DataGridView1.DataSource = dt End Using End Sub Public Sub ExportDataGridView(ByVal srcDgv As DataGridView) Dim path As Object = "D:\test.docx" Dim none As Object = System.Reflection.Missing.Value Dim wordApp As Word.Application = New Word.Application() Dim document As Word.Document = wordApp.Documents.Add(none, none, none, none) Dim table As Word.Table = document.Tables.Add(document.Paragraphs.Last.Range, DataGridView1.RowCount, DataGridView1.ColumnCount) With table .Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle .Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle End With Try For i As Integer = 0 To srcDgv.Columns.Count - 1 table.Cell(0, i + 1).Range.Text = srcDgv.Columns(i).HeaderText

    Next For i As Integer = 1 To srcDgv.Rows.Count - 1 For j As Integer = 0 To srcDgv.Columns.Count - 1 table.Cell(i + 1, j + 1).Range.Text = srcDgv(j, i - 1).Value.ToString() Next Next document.SaveAs(path, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none) document.Close(none, none, none) MessageBox.Show("success") Finally wordApp.Quit(none, none, none) End Try End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ExportDataGridView(DataGridView1) End Sub End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    I tested your code , it results an error : The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

    This is related to this code line  :

     table.Cell(0, i + 1).Range.Text = srcDgv.Columns(i).HeaderText
                


    • Edited by Kevin993 Thursday, September 27, 2018 8:49 AM
    Thursday, September 27, 2018 8:48 AM
  • Hi,

    I have not reported an error here.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kevin993 Sunday, September 30, 2018 10:12 PM
    Thursday, September 27, 2018 9:35 AM