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.

The answer to the exercise will be included and explained if you attend the Wise Owl course listed below!

Category ==> Advanced VBA  (29 exercises)
Topic ==> Links to other applications  (4 exercises)
Level ==> Relatively easy
Course ==> Advanced VBA
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

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:

List of children

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:

Dear Santa letter

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:

  1. Opens the Word document.
  2. Goes to the Present bookmark and writes in the child's present request.
  3. Goes to the ChildName bookmark and writes in the child's name.
  4. Saves this document, using the child's name as a file name.

You may find the following macro useful as a hint:

Sub 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

doc.Activate

'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"

doc.Close

End Sub

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! 

This page has 0 threads Add post