Una vez que el DataGridView este lleno crearemos un botón que llamara la función:
Private Sub impresion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles impresion.Click
Call DataGridView_a_word(DataGridView1)
End Sub
Copiamos la función que envía el contenido del DataGridView a Word:
Public Function DataGridView_a_word(ByVal midatagridview As DataGridView)
Dim Word As Application
Dim Doc As Document
Dim Table As Table
Dim Rng As Range
'para los titulos
Dim titulo1 As Paragraph
Dim titulo2 As Paragraph
Dim titulo3 As Paragraph
Dim titulo4 As Paragraph
Word = CreateObject("Word.Application")
Word.Visible = False
Doc = Word.Documents.Add
titulo1 = Doc.Content.Paragraphs.Addtitulo1.Range.Text = "REPORTE MENSUAL"
titulo1.Range.Font.Color = WdColor.wdColorBlue
titulo1.Range.Font.Bold = True
titulo1.Format.SpaceAfter = 24
titulo1.Range.InsertParagraphAfter()titulo2 = Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item("\endofdoc").Range)
titulo2.Range.Text = "FOLIO: " + Me.Label_folio.Text
titulo2.Range.Font.Color = WdColor.wdColorBlack
titulo2.Format.SpaceAfter = 6
titulo2.Range.InsertParagraphAfter() titulo3 = Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item("\endofdoc").Range)
titulo3.Range.Text = "NOMBRE: " + Me.TextBox_nombre.Text
titulo3.Range.Font.Color = WdColor.wdColorBlack
titulo3.Format.SpaceAfter = 6
titulo3.Range.InsertParagraphAfter()titulo4 = Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item("\endofdoc").Range)
titulo4.Range.Text = "FECHA: " + Me.DateTimePicker_fecha.Text
titulo4.Range.Font.Color = WdColor.wdColorBlack
titulo4.Format.SpaceAfter = 6
titulo4.Range.InsertParagraphAfter()Dim v_columna As Integer = midatagridview.ColumnCount
Dim v_renglon As Integer = midatagridview.RowCount
Table = Doc.Tables.Add(Doc.Bookmarks.Item("\endofdoc").Range, midatagridview.RowCount, midatagridview.ColumnCount)
'Agregando los campos de midatagridview
For i As Integer = 1 To v_columna
Table.Cell(1, i).Range.Text = midatagridview.Columns(i - 1).Name.ToString
Next
'Agregando los registros a la tabla
For v_fila As Integer = 0 To v_renglon - 1 'antes -2
For Col As Integer = 0 To v_columna - 1
Dim colType As Type = DataGridView1.Columns(Col).GetType
'agrega los registros del datagridviw
If colType.Name = "DataGridViewImageColumn" Then
'en caso de encontrar una imagen en el datagridview la agrega
Dim image As Image = DirectCast(DataGridView1.Rows(v_fila).Cells(Col).Value, Image)
Clipboard.SetImage(image)
Table.Cell(v_fila + 1, Col + 1).Range.Paste()
Else
Table.Cell(v_fila + 1, Col + 1).Range.Text = DataGridView1.Rows(v_fila).Cells (Col).Value.ToString()
End If
Next
Next
'Negrita y cursiva
Table.Rows.Item(1).Range.Font.Bold = False
Table.Rows.Item(1).Range.Font.Italic = False
'Borde de la tabla
Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDot
Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDot
Table.Borders.InsideColor = WdColor.wdColorBlue
Rng = Doc.Bookmarks.Item("\endofdoc").Range
Rng.InsertParagraphAfter()
Rng.InsertAfter(".")
MessageBox.Show("Formato generado")
Word.Visible = True
End Function
0 Comentarios