WISE OWL EXERCISES
ADVANCED VBA EXERCISES
Advanced VBA | Links to other applications exercise | Using an Excel macro to create a Dear Santa letter in Word
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You can learn how to do this exercise if you attend one of more of the courses listed below!
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Open the Excel workbook in the above folder - it contains the names of the children for whom you have to buy presents, and what they want:
Someone has thoughtfully created a range name called Children.
Open the Word document in the above folder, and you'll see that it contains a (brief) letter to Santa, containing two bookmarks:
The idea is that your Excel macro will substitute in each child's present request at the first bookmark, and their name at the second one.
The bookmarks have the following names:
|Bookmark||What will be put there|
|Present||The name of the desired present|
|ChildName||The name of the child requesting this|
Close this Word document down, and create a macro in the Excel workbook which loops over the children's names, and for each:
- Opens the Word document.
- Goes to the Present bookmark and writes in the child's present request.
- Goes to the ChildName bookmark and writes in the child's name.
- Saves this document, using the child's name as a file name.
You may find the following macro useful as a hint:
Dim doc As Document
'open up the Word document, getting a reference to it
Set doc = Documents.Open("E:\Referencing applications\Dear Santa.docx")
'go to this document
'write in a present name
doc.Bookmarks("Present").Range.Text = "A really fluffy owl"
'write in the child's name
doc.Bookmarks("ChildName").Range.Text = "Wally Owl"
'save this document, and close it
doc.SaveAs2 "E:\Referencing applications\Wally Owl"
Test that your macro contains one letter for each child, then save it as Suffer the little children.xlsm and close it down, reflecting on another tedious Christmas task now automated!