Ive inherited the following code
Summary
WinForms displays a list of users that have attended a course
Students either passed (X) or failed (Y) a course.
It prints a document list of all students that completed the course
It prints certificates for students that passed the course.
The issue is
With printing certificates it doesn’t print the last student certificate
It appears the counters are out, and drops / ignores the last student when printing certificates.
The same code (and counters) are used to print the list students completed the course and prints the certificates.
However when I debug the results are inconsistent
Sometimes when I debug, the certificate for the last student doesn’t print
Othertimes, when I debug, its accurate and prints all certificates for all users that passed
Any ideas
TIA
Data
Course location Completed Date Course Name Student Name Result Passed(X) or Failed (Y )
Location 1 24/05/2019 CourseXYZ Student1 X
Location 1 24/05/2019 CourseXYZ Student2 X
Location 1 24/05/2019 CourseXYZ Student3 X
Location 1 24/05/2019 CourseXYZ Student4 Y
code
Summary
WinForms displays a list of users that have attended a course
Students either passed (X) or failed (Y) a course.
It prints a document list of all students that completed the course
It prints certificates for students that passed the course.
The issue is
With printing certificates it doesn’t print the last student certificate
It appears the counters are out, and drops / ignores the last student when printing certificates.
The same code (and counters) are used to print the list students completed the course and prints the certificates.
However when I debug the results are inconsistent
Sometimes when I debug, the certificate for the last student doesn’t print
Othertimes, when I debug, its accurate and prints all certificates for all users that passed
Any ideas
TIA
Data
Course location Completed Date Course Name Student Name Result Passed(X) or Failed (Y )
Location 1 24/05/2019 CourseXYZ Student1 X
Location 1 24/05/2019 CourseXYZ Student2 X
Location 1 24/05/2019 CourseXYZ Student3 X
Location 1 24/05/2019 CourseXYZ Student4 Y
code
VB.NET:
Invoke(New DelProgressUpdate(AddressOf ProgressUpdate), "Spooling process...", 0, 0)
Dim objDTList As New List(Of DataTable)
For i As Integer = 0 To dgImportedData.Rows.Count - 1
If dgImportedData.Rows(i).Visible = True Then
Dim boolFound As Boolean = False
For ii As Integer = 0 To objDTList.Count - 1
' if course DataTableList CompletedDate is the same as DataGridList CompletedDate
' AND if course DataTableList CourseName is the same as DataGridList CourseName
If (objDTList(ii).Rows(0).Item("CompletedDate").ToString = dgImportedData.Item("dgCompletedDate", i).Value) And (objDTList(ii).Rows(0).Item("CourseName").ToString = dgImportedData.Item("dgCourseName", i).Value) = True Then
boolFound = True
Exit For
End If
Next
If boolFound = False Then
Dim objDT As New DataTable
objDT.Columns.Add("CompletedDate")
objDT.Columns.Add("CourseName")
objDT.Rows.Add(dgImportedData.Item("dgCompletedDate", i).Value, dgImportedData.Item("dgCourseName", i).Value)
objDTList.Add(objDT)
objDT.Dispose()
End If
End If
Next
' write the list completed the course
For i As Integer = 0 To objDTList.Count - 1
Invoke(New DelProgressUpdate(AddressOf ProgressUpdate), "Processing completion list - " & (i + 1) & " of " & objDTList.Count, objDTList.Count, (i + 1))
Dim objDoc As Microsoft.Office.Interop.Word.Document = objWordApp.Documents.Open(Application.StartupPath & "\Templates\Course Completion.docx", Nothing, True)
objDoc = objWordApp.ActiveDocument
For ii As Integer = 0 To dgImportedData.Rows.Count - 1
If dgImportedData.Rows(ii).Visible = True Then
If (objDTList(i).Rows(0).Item("CompletedDate").ToString = dgImportedData.Item("dgCompletedDate", ii).Value) And (objDTList(i).Rows(0).Item("CourseName").ToString = dgImportedData.Item("dgCourseName", ii).Value) = True Then
' if student passed the course add to the list
If dgImportedData.Item("dgResult", ii).Value.ToString.ToUpper = "X" Then
objDoc.Content.Find.Execute(FindText:="%INSTRUCTOR%", ReplaceWith:="" & objDTTrainingInstructors.Rows(Params(1)).Item("FullName") & " " & objDTTrainingInstructors.Rows(Params(1)).Item("Rank") & " " & objDTTrainingInstructors.Rows(Params(1)).Item("RegisteredNo") & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%COURSENAME%", ReplaceWith:="" & objDTList(i).Rows(0).Item("CourseName").ToString & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%DATECOMPLETED%", ReplaceWith:="" & Format(CDate(objDTList(i).Rows(0).Item("CompletedDate").ToString), "dd/MM/yyyy") & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%LOCATION%", ReplaceWith:="" & Params(3) & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Tables(objDoc.Tables.Count).Rows.Add()
objDoc.Tables(objDoc.Tables.Count).Rows(objDoc.Tables(objDoc.Tables.Count).Rows.Count).Range.Font.Bold = False
objDoc.Tables(objDoc.Tables.Count).Cell(objDoc.Tables(objDoc.Tables.Count).Rows.Count, 1).Range.Text = dgImportedData.Item("dgName", ii).Value
End If
End If
End If
Next
objDoc.PrintOut(Copies:=objPrintDialog.PrinterSettings.Copies, Background:=True)
objDoc.Saved = True
objDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges)
objDoc = Nothing
Next i
Invoke(New DelProgressUpdate(AddressOf ProgressUpdate), "", 0, 0)
' write the course certificate
For i As Integer = 0 To dgImportedData.Rows.Count - 1
Invoke(New DelProgressUpdate(AddressOf ProgressUpdate), "Processing certificate - " & (i + 1) & " of " & dgImportedData.Rows.Count, dgImportedData.Rows.Count, (i + 1))
If dgImportedData.Rows(i).Visible = True Then
' if student passed the course write the certificate
If dgImportedData.Item("dgResult", i).Value.ToString.ToUpper = "X" Then
Dim objDoc As Microsoft.Office.Interop.Word.Document = objWordApp.Documents.Open(Application.StartupPath & "\Templates\Certificate.docx", Nothing, True)
objDoc = objWordApp.ActiveDocument
objDoc.Content.Find.Execute(FindText:="%NAME%", ReplaceWith:="" & dgImportedData.Item("dgName", i).Value.ToString.ToUpper & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%COURSENAME%", ReplaceWith:="" & dgImportedData.Item("dgCourseName", i).Value & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%ENDDATE%", ReplaceWith:="" & Format(CDate(dgImportedData.Item("dgCompletedDate", i).Value.ToString), "d MMMM yyyy") & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%AUTHORISINGOFFICER%", ReplaceWith:="" & objDTAuthorisingOfficers.Rows(Params(2)).Item("FullName") & " " & objDTAuthorisingOfficers.Rows(Params(2)).Item("RegisteredNo"), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.PrintOut(Copies:=objPrintDialog.PrinterSettings.Copies, Background:=True)
objDoc.Saved = True
objDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges)
objDoc = Nothing
End If
End If
Next
objDTList = Nothing
Last edited by a moderator: