Option Explicit
Sub SendToWord()
'Declare Variables for This Automation
Dim wd As Word.Application ' Word Applicaton
Dim wdDOC As Word.Document 'Word Document
Dim iRow As Long 'Variable to hold the starting row and loop through all records in the table
Dim PercentageScore As Variant 'variable to hold the percentage value
Dim sh As Worksheet ' worksheet variable to refer the sheet where scores are available
'Start Word and add a new document
Set wd = New Word.Application
'Set worksheet where a table is avaialble
Set sh = ThisWorkbook.Sheets("Student Scores")
'Intialize iRow with 6 as data are starting from row number 6 in table
iRow = 6
Do While sh.Range("A" & iRow).Value <> ""
'Opening the word template where bookmarks have been added
Set wdDOC = wd.Documents.Open(ThisWorkbook.Path & "\Marksheet Template.docx")
wd.Visible = False
'Name
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Name"
wd.Selection.TypeText Text:=sh.Range("A" & iRow).Value
'Registration_Number
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Registration_Number"
wd.Selection.TypeText Text:=sh.Range("B" & iRow).Value
'Program_Name
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Program_Name"
wd.Selection.TypeText Text:=sh.Range("C" & iRow).Value
'Examination_Date
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Examination_Date"
wd.Selection.TypeText Text:=Format(sh.Range("D" & iRow).Value, "dd-mmm-yy")
'Grade
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Grade"
wd.Selection.TypeText Text:=sh.Range("P" & iRow).Value
'Statistics_Marks
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Statistics_Marks"
wd.Selection.TypeText Text:=sh.Range("E" & iRow).Value
'Statistics_Result
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Statistics_Result"
wd.Selection.TypeText Text:=sh.Range("F" & iRow).Value
'Excel_Marks
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Excel_Marks"
wd.Selection.TypeText Text:=sh.Range("G" & iRow).Value
'Excel_Result
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Excel_Result"
wd.Selection.TypeText Text:=sh.Range("H" & iRow).Value
'VBA_Marks
wd.Selection.GoTo What:=wdGoToBookmark, Name:="VBA_Marks"
wd.Selection.TypeText Text:=sh.Range("I" & iRow).Value
'VBA_Result
wd.Selection.GoTo What:=wdGoToBookmark, Name:="VBA_Result"
wd.Selection.TypeText Text:=sh.Range("J" & iRow).Value
'SQL_Marks
wd.Selection.GoTo What:=wdGoToBookmark, Name:="SQL_Marks"
wd.Selection.TypeText Text:=sh.Range("K" & iRow).Value
'SQL_Result
wd.Selection.GoTo What:=wdGoToBookmark, Name:="SQL_Result"
wd.Selection.TypeText Text:=sh.Range("L" & iRow).Value
'PowerBI_Marks
wd.Selection.GoTo What:=wdGoToBookmark, Name:="PowerBI_Marks"
wd.Selection.TypeText Text:=sh.Range("M" & iRow).Value
'PowerBI_Result
wd.Selection.GoTo What:=wdGoToBookmark, Name:="PowerBI_Result"
wd.Selection.TypeText Text:=sh.Range("N" & iRow).Value
'GrandTotal
wd.Selection.GoTo What:=wdGoToBookmark, Name:="GrandTotal"
wd.Selection.TypeText Text:=sh.Range("O" & iRow).Value
'Calculating Percentage
PercentageScore = Format(sh.Range("O" & iRow).Value / 500, "0.0%")
'Percentage
wd.Selection.GoTo What:=wdGoToBookmark, Name:="Percentage"
wd.Selection.TypeText Text:=PercentageScore
'Clear the Bookmarks from this file
On Error Resume Next
wdDOC.Bookmarks("Name").Delete
wdDOC.Bookmarks("Registration_Number").Delete
wdDOC.Bookmarks("Program_Name").Delete
wdDOC.Bookmarks("Examination_Date").Delete
wdDOC.Bookmarks("Grade").Delete
wdDOC.Bookmarks("Statistics_Marks").Delete
wdDOC.Bookmarks("Statistics_Result").Delete
wdDOC.Bookmarks("Excel_Marks").Delete
wdDOC.Bookmarks("Excel_Result").Delete
wdDOC.Bookmarks("VBA_Marks").Delete
wdDOC.Bookmarks("VBA_Result").Delete
wdDOC.Bookmarks("SQL_Marks").Delete
wdDOC.Bookmarks("SQL_Result").Delete
wdDOC.Bookmarks("PowerBI_Marks").Delete
wdDOC.Bookmarks("PowerBI_Result").Delete
wdDOC.Bookmarks("GrandTotal").Delete
wdDOC.Bookmarks("Percentage").Delete
'Save the document with Student's name
wdDOC.SaveAs2 (ThisWorkbook.Path & "\" & sh.Range("A" & iRow).Value & ".docx")
'Close the document
wdDOC.Close
Set wdDOC = Nothing
iRow = iRow + 1
Loop
wd.Quit
Set wd = Nothing
MsgBox "Mark-sheets have been prepared for all the students."
End Sub
No comments: