Professional Documents
Culture Documents
By
TheDataLabs
-
April 26, 2020
In this article, we will learn to automate Microsoft Word from Excel with the help of Visual
Basic Language. To learn the Word Automation, we will consider a very practical example i.e.
Creating Student’s Mark Sheet in MS Word from Excel Table.
If you find that you are constantly copying and pasting data into Microsoft Word in a specific
format then you can use the code and logic, which we are going to use here.
Please create a Word file for Students mark sheet as mentioned in below image or download the
template used in this tutorial.
Once you create or download the word file, you need to create bookmarks tagging the location
where you want your Excel data to be copied.
In current word file, we need total 17 bookmarks to transfer Student Name, Registration
Number, Program Name, Examination Date, Grade, Statistics Marks, Statistics Result, Excel
Marks, Excel Result, VBA Marks, VBA Result, SQL Marks, SQL Result, Power BI Mark,
Power BI Result, Grand Total Marks and Percentage.
Bookmark Fields Highlighted with red
To create a bookmark in a Word document, place your cursor where you want the bookmark
then go to Insert Tab and select Bookmark available under the Links group. This will open the
Bookmark dialog box where you can provide the name of bookmark and click on Add button.
Let us follow the same step as mentioned above and create bookmark for ‘Name’.
Steps to Insert Bookmarks
In the same way, just create rest of the 16 Bookmarks required for this automation.
Please use the below mentioned Bookmarks name. We will use these names in VBA code while
doing automation.
1. Name
2. Registration_Number
3. Program_Name
4. Examination_Date
5. Grade
6. Statistics_Marks
7. Statistics_Result
8. Excel_Marks
9. Excel_Result
10. VBA_Marks
11. VBA_Result
12. SQL_Marks
13. SQL_Result
14. PowerBI_Marks
15. PowerBI_Result
16. GrandTotal
17. Percentage
Once, you create all the Bookmarks save the word file in the name ‘Marksheet Template.docx‘
at the same location where you are going to keep MS Excel file. Now, close the file after saving
it.
Now, open the Excel Application and create a blank worksheet. Save the file with the name
‘Automating Word from Excel.xlsm’ in the same folder where you have kept Word file.
Create the table with required data as mentioned in below image. Please keep the cell reference
as it is otherwise your code will not work (if you are following the same example).
Now, insert a rounded rectangle and give the caption as ‘Prepare Marksheet’. We will assign
macro on this button to create mark sheet once coding will be done.
Prepare Excel File and Table
Let us jump to Visual Basic Application Window to start coding. To open VBA Window, go to
Developer Tab and Click on Visual Basic button available under Code group.
As we are going to automate MS Word from Excel hence, we need to provide the reference of
MS Word Object library. To add the reference, click on Tools menu and then select
References…
In references window, select ‘Microsoft Word 16.0 Object Library’ from the available references
and then click on Okay.
Add Microsoft Word 16.0 Object Library
Once you done with adding references then double click on Module1 to open the code window.
Start Coding
Option Explicit
Sub SendToWord()
'Declare Variables for This Auto
1 Option Explicit
2 Sub SendToWord()
3 'Declare Variables for This Automation
4
5 Dim wd As Word.Application ' Word Applicaton
6 Dim wdDOC As Word.Document 'Word Document
7 Dim iRow As Long 'Variable to hold the starting row and loop through all records in the
8 table
9 Dim PercentageScore As Variant 'variable to hold the percentage value
10
11 Dim sh As Worksheet ' worksheet variable to refer the sheet where scores are available
12
13 'Start Word and add a new document
14 Set wd = New Word.Application
15
16 'Set worksheet where table is avaialble
17 Set sh = ThisWorkbook.Sheets("Student Scores")
18
19 'Intialize iRow with 6 as data are starting from row number 6 in table
20 iRow = 6
21
22 Do While sh.Range("A" & iRow).Value <> ""
23
24 'Opening the word template where bookmarks have been added
25 Set wdDOC = wd.Documents.Open(ThisWorkbook.Path & "\Marksheet
26 Template.docx")
27 wd.Visible = False
28
29 'Name
30 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Name"
31 wd.Selection.TypeText Text:=sh.Range("A" & iRow).Value
32
33 'Registration_Number
34 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Registration_Number"
35 wd.Selection.TypeText Text:=sh.Range("B" & iRow).Value
36
37 'Program_Name
38 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Program_Name"
39 wd.Selection.TypeText Text:=sh.Range("C" & iRow).Value
40
41 'Examination_Date
42 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Examination_Date"
43 wd.Selection.TypeText Text:=Format(sh.Range("D" & iRow).Value, "dd-mmm-
44 yy")
45
46 'Grade
47 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Grade"
48 wd.Selection.TypeText Text:=sh.Range("P" & iRow).Value
49
50 'Statistics_Marks
51 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Statistics_Marks"
52 wd.Selection.TypeText Text:=sh.Range("E" & iRow).Value
53
54 'Statistics_Result
55 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Statistics_Result"
56 wd.Selection.TypeText Text:=sh.Range("F" & iRow).Value
57
58 'Excel_Marks
59 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Excel_Marks"
60 wd.Selection.TypeText Text:=sh.Range("G" & iRow).Value
61
62 'Excel_Result
63 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Excel_Result"
64 wd.Selection.TypeText Text:=sh.Range("H" & iRow).Value
65
66 'VBA_Marks
67 wd.Selection.GoTo What:=wdGoToBookmark, Name:="VBA_Marks"
68 wd.Selection.TypeText Text:=sh.Range("I" & iRow).Value
69
70 'VBA_Result
71 wd.Selection.GoTo What:=wdGoToBookmark, Name:="VBA_Result"
72 wd.Selection.TypeText Text:=sh.Range("J" & iRow).Value
73
74 'SQL_Marks
75 wd.Selection.GoTo What:=wdGoToBookmark, Name:="SQL_Marks"
76 wd.Selection.TypeText Text:=sh.Range("K" & iRow).Value
77
78 'SQL_Result
79 wd.Selection.GoTo What:=wdGoToBookmark, Name:="SQL_Result"
80 wd.Selection.TypeText Text:=sh.Range("L" & iRow).Value
81
82 'PowerBI_Marks
83 wd.Selection.GoTo What:=wdGoToBookmark, Name:="PowerBI_Marks"
84 wd.Selection.TypeText Text:=sh.Range("M" & iRow).Value
85
86 'PowerBI_Result
87 wd.Selection.GoTo What:=wdGoToBookmark, Name:="PowerBI_Result"
88 wd.Selection.TypeText Text:=sh.Range("N" & iRow).Value
89
90 'GrandTotal
91 wd.Selection.GoTo What:=wdGoToBookmark, Name:="GrandTotal"
92 wd.Selection.TypeText Text:=sh.Range("O" & iRow).Value
93
94 'Calculating Percentage
95 PercentageScore = Format(sh.Range("O" & iRow).Value / 500, "0.0%")
96
97 'Percentage
98 wd.Selection.GoTo What:=wdGoToBookmark, Name:="Percentage"
99 wd.Selection.TypeText Text:=PercentageScore
100
101 'Clear the Bookmarks from this file
102
103 On Error Resume Next
104
105 wdDOC.Bookmarks("Name").Delete
106 wdDOC.Bookmarks("Registration_Number").Delete
107 wdDOC.Bookmarks("Program_Name").Delete
wdDOC.Bookmarks("Examination_Date").Delete
wdDOC.Bookmarks("Grade").Delete
108 wdDOC.Bookmarks("Statistics_Marks").Delete
109 wdDOC.Bookmarks("Statistics_Result").Delete
110 wdDOC.Bookmarks("Excel_Marks").Delete
111 wdDOC.Bookmarks("Excel_Result").Delete
112 wdDOC.Bookmarks("VBA_Marks").Delete
113 wdDOC.Bookmarks("VBA_Result").Delete
114 wdDOC.Bookmarks("SQL_Marks").Delete
115 wdDOC.Bookmarks("SQL_Result").Delete
116 wdDOC.Bookmarks("PowerBI_Marks").Delete
117 wdDOC.Bookmarks("PowerBI_Result").Delete
118 wdDOC.Bookmarks("GrandTotal").Delete
119 wdDOC.Bookmarks("Percentage").Delete
120
121 'Save the document with Student's name
122 wdDOC.SaveAs2 (ThisWorkbook.Path & "\" & sh.Range("A" &
123 iRow).Value & ".docx")
124
125 'Close the document
126 wdDOC.Close
127
128 Set wdDOC = Nothing
129
130 iRow = iRow + 1
131
132 Loop
133
134 wd.Quit
135 Set wd = Nothing
136
137 MsgBox "Mark-sheets have been prepared for all the students."
End Sub
Now move to Excel Application and assign the macro on button available on top of the table.
Assign Macro
You can test this application with the data available in table.