Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
Public Class carta
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim Documento As Word.Document
' Public Function
Sub GrillaAWord(ByVal Grilla As DataGridView)
Dim NCol As Integer = Grilla.ColumnCount
Dim NRow As Integer = Grilla.RowCount
Dim Table As Table
Table = Documento.Tables.Add(Documento.Bookmarks.Item("marcador4").Range, Grilla.RowCount, Grilla.ColumnCount)
'Agregando Los campos De La Grilla
For i As Integer = 1 To NCol
Table.Cell(1, i).Range.Text = Grilla.Columns(i - 1).Name.ToString
Next
'Agregando Los Registros A La Tabla
For Fila As Integer = 0 To NRow - 2
For Col As Integer = 0 To NCol - 1
If Grilla.Rows(Fila).Cells(Col).Value IsNot DBNull.Value Then
Table.Cell(Fila + 2, Col + 1).Range.Text = Grilla.Rows(Fila).Cells(Col).Value
End If
Next
Next
'Nombres De Los Campos
Table.Rows.Item(1).Range.Font.Bold = True
Table.Rows.Item(1).Range.Font.Size = 11
Table.Rows.Item(1).Range.Font.Name = "Arial"
Table.Rows.Item(1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
'Boder De La Tabla
Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle
Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle
'Table.Borders.InsideColor = WdColor.wdColorBlue
End Sub
Sub controles()
If dgv.Rows.Count < 0 Then
btgenerar.Enabled = False
Else
btgenerar.Enabled = True
End If
End Sub
Sub cargardgv()
Try
conexion.Open()
ds = New DataSet
Dim table As DataTableCollection = ds.Tables
da = New OleDbDataAdapter("SELECT t_alumnos.no_control.... WHERE t_profesores.id_tutor = '" & cbtutor.SelectedValue & "' AND t_asistencias.semestre_1 Is Null", conexion)
da.Fill(ds, "consulta1")
'Asignamos
dgv.DataSource = New DataView(table(0))
dgv.Columns(1).Width = 200
dgv.Columns(2).Width = 210
dgv.Refresh()
'Liberamos
ds.Dispose()
da.Dispose()
conexion.Close() 'Cerramos
Catch ex As Exception
ex.Message.ToUpper()
End Try
End Sub
Sub cargartutores()
Dim DA As New OleDb.OleDbDataAdapter("SELECT * FROM t_profesores", conexion)
Dim DS As New DataSet
DA.Fill(DS, "t_profesores")
cbtutor.ValueMember = "id_tutor"
cbtutor.DisplayMember = "nombre_completo"
cbtutor.DataSource = DS.Tables("t_profesores")
End Sub
Private Sub carta_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
conexion.Close()
End Sub
Private Sub carta_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cargartutores()
btgenerar.Enabled = False
'ruta de la plantilla (contiene los marcadores)
txtdireccion.Text = "C:\plantilla_carta.docx"
'ruta del documento que se va a generar de los datos insertados en la plantilla
txtdocumento.Text = "C:\carta1.docx"
End Sub
Private Sub cbtutor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbtutor.SelectedIndexChanged
cbtutor.DropDownStyle = ComboBoxStyle.DropDownList
btgenerar.Enabled = False
End Sub
Private Sub cbsemestre_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbsemestre.SelectedIndexChanged
cbsemestre.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Private Sub btver_Click(sender As Object, e As EventArgs) Handles btver.Click
cargardgv()
btgenerar.Enabled = False
controles()
End Sub
Private Sub btgenerar_Click(sender As Object, e As EventArgs) Handles btgenerar.Click
Dim MSWord As New Word.Application
FileCopy(txtdireccion.Text, txtdocumento.Text)
Documento = _
MSWord.Documents.Open(txtdocumento.Text)
Documento.Bookmarks.Item("marcador1").Range.Text = dtfecha.Text
Documento.Bookmarks.Item("marcador2").Range.Text = cbtutor.Text
Documento.Bookmarks.Item("marcador3").Range.Text = cbsemestre.Text
GrillaAWord(dgv)
MSWord.Visible = True
End Sub
End Class