I'm currently making a system where there's a need to print a student's transcript of records. The grades of the student will be retrieved from a MySQL database. I want to find and replace words in the Word document without showing the actual windows of MS Word.
Here is my code:
Here is my code:
Code:
Dim dsGrade As DataSet, daGrade As MySqlDataAdapter, cbGrade As MySqlCommandBuilder
Dim strSQL As String, strConnection As String = "server=localhost;uid=root;pwd=root;database=nazarene;"
Dim sqlConnection As New MySqlConnection(strConnection)
Dim I As Integer, strSelectedStudent() As String, strStudentNumber As String
Dim objWordApp As New Word.Application
objWordApp.Visible = True
Dim objDoc As Word.Document = objWordApp.Documents.Open(Environment.CurrentDirectory & "\doc\wrdTOR.docx")
objDoc = objWordApp.ActiveDocument
sqlConnection.Open()
strSQL = "SELECT * FROM grade1"
daGrade = New MySqlDataAdapter(strSQL, sqlConnection)
cbGrade = New MySqlCommandBuilder(daGrade)
dsGrade = New DataSet
daGrade.Fill(dsGrade, "grade1")
strSelectedStudent = Strings.Split(lstStudent.SelectedItem, "-")
strStudentNumber = Strings.Trim(strSelectedStudent(0))
With dsGrade.Tables("grade1")
For I = 0 To .Rows.Count - 1
If .Rows(I).Item("studentID") = strStudentNumber Then
objDoc.Content.Find.Execute(FindText:="g1SchoolFrom", MatchWholeWord:=True, ReplaceWith:=.Rows(I).Item("schoolFrom"), Replace:=Word.WdReplace.wdReplaceOne)
objDoc.Content.Find.Execute(FindText:="g1SchoolYear", MatchWholeWord:=True, ReplaceWith:=.Rows(I).Item("schoolYear"), Replace:=Word.WdReplace.wdReplaceOne)
objDoc.Content.Find.Execute(FindText:="A1", MatchWholeWord:=True, ReplaceWith:=.Rows(I).Item("p1English"), Replace:=Word.WdReplace.wdReplaceOne)
' the rest of the code
' the rest of the code
' the rest of the code