There are certain reports and documents that will always need to be output in MS Word and ultimately into a PDF. The problems come when reports that are heavy on calculations in Excel need to be placed into a report in Word. We want to avoid as many cut and pastes and hard coding as this is when errors will occur.
It would interesting to see how many invoices and send out with errors or shipping notes that have a financial impact.
You know it is very possible to control the whole reporting or document in Word through Excel. And with a little patience and concentration, I will impart the skills you need to set up for yourself and rid yourself of the monotony and error risk forever.
Please feel free to contact me with your feedback on this training or any other training at XLDB Solutions, and also if you have any issues or tasks in Excel you would like me to target in future for solutions. email: email@example.com
1. Setup a Control Sheet
The control sheet is a data validation and a button which will run a macro. It can be as simple or as complex as you need to make it.
In the example above there is simply a Data Validation cell which is linked to a list of shipment numbers and a Button to run the reports VBA subroutine
The Produce Word Report button is linked to the MergeMe VBA subroutine that is shown later.
2. Setup a Transfer Sheet
2.1 Create a worksheet and name it Transfer
2.2 The Transfer sheet needs to have two 2 rows
2.2.1 Row 1: Has a list of field names, These will be used in Word later so use something meaningful.
2.2.2 Row 2: Has a list of the records which will be transferred to Word. The cells will contain sufficient formulas to extract the relevant records from the Datasheet using the Data validation on the Control Sheet.
2.3 Create sufficient fields to ensure that all records required in Word are set up or retrieved like Fig1.0 The order of the fields isn’t important as the field names are used for the transfer, not the order.
Note: The format or layout doesn’t matter as this is controlled in Word.
Notice on the Transfer sheet that the Top Row is a list of field names The second row uses an Index(Match( )) combination to retrieve the relevant records from the Datasheet.
2.4 Save the Excel file.
3.0 Setup a Word template
3.1 Set up in Word a file which will be used as a template for the import. Leave gaps where your field values will go.
3.2 Save the file
3.3 Open the example file (Shipping Template.docx) If this is the first time you have opened the example file it may prompt you “Opening this file will run the following SQL File …”
3.4 This is ok so accept Yes
3.5 You can either accept that and then follow the links to connect the file to the Production records.xlsm file It will then prompt you for the Data Table which in our case is Transfer$ ie: the Sheet Name with a $ sign at the end.
3.6 If it didn’t prompt you above or you answered No to the “Opening this file will run the following SQL File …” prompt we will connect again later anyway.
3.7 Now set up the file in Word with all the text graphics, lines colours etc required for your form/report.
Leave gaps for the fields which we will add next.
4.0 Adding fields
4.1 Use mail merge to open the data source (Production records.xlsm)
4.2 Goto Mailings, Select Recipients, Use Existing List…
4.3 Navigate to the Production Records.xlsm file after which it will prompt you for the table which in our case is Transfer$ ie: the Sheet Name with a $ sign at the end (as discussed above)
4.4 Move to the 4 missing Field Locations as per the following table and insert the Field Names using the
4.5 Mailings, Insert Merge Field tab.
4.6 Insert Fields as per the following table and highlights above:
|Location||Field Name (from Production Records.xlsm)|
|Shipment No :||Shipment_No|
The other fields Destination, Form and Date have already been pre-entered and are shown in Blue You can format the fields as required, select the entire field and change the font, colours etc to suit. You can view the field values using the Preview Results Button, see below.
4.7 Make any other changes to the file
Save the file as a Word File Shipping Template.docx (not as a Word Template *.dotx)
4.8 Close Word
5.0 Run the Report
5.1 In the Production records.xlsm file we will now link the macro to the button on the control sheet and check 3 lines in the macro before we execute it.
5.2 Goto VBA using Alt F11
5.3 Select the Production Records.xlsm, Modules on the left and find the
Sub MergeMe() Subroutine on the right
Near the top of the subroutine are 2 lines which list both the file which Word will use as a template and what the new file will be saved as after merging.
‘ Setup filenames
Const WTempName = “Shipping Template.docx“ ‘This is the Word Templates name, Change as req’d
Const NewFileName = “New Certificate.docx” ‘This is the New Word Documents File Name, Change as req’d
Change these values as appropriate
The code will overwrite the existing output file if it exists so once executes save it to another name/location.
If you are using your own data file copy this subroutine to your own VBA Module and edit as above.
5.4 You can now go back to Excel (Alt F11) and execute the macro using the button on the control page.
5.5 You should now have a new file called New Certificate.docx in the same directory as the Sample files.
Now you will have your own Excel driving a Word document that you can customise for any type of Word report.
Just in case you have not been able to get to this position, I have the whole templates ready for you which you can use. Download from here: