NOTE: These instructions only work for Word 2007 and later; they use Building Blocks, which are only available in these versions of Word. If you are using Word 2003 or earlier, I hope to share a much longer macro with you in a few weeks that does something similar but without the formatting.
Scenario
You need to make it easy for your authors to insert many tables in their documents. So you need to add something to the template that allows them to press a couple of keys and get both a preformatted table and an automated caption inserted.
The tables all need to be formatted the same — same header row shading, height alignment and style; same border color and width; same table row height, alignment and style. Once they’ve inserted a table, the author has to be able to modify it (e.g. merge cells) without those modifications affecting anything else.
Your template uses outline heading numbering, so you want a table caption to be inserted at the same time as you insert the table. The caption is to include the chapter number and a sequential caption number (e.g. if they’re in section 2, you want the tables they insert in section 2 to be numbered Table 2-1, Table 2-2 etc.).
Solution
While Quick Tables are an option, you won’t get the caption inserted with them. So this solution takes Quick Tables a couple of steps further.
Step 1: Set up the table
- Open your Word template — the template itself, NOT a document based on the template.
- Create a table in your template and format it as you want. I suggest you create a table that’s four column by five rows — authors can add/remove rows and columns later.
When creating your table, make sure you:
- apply the relevant styles to the table header row and the table rows
- set the row heights for the table header row and the table rows
- set the alignments for the table header row and the table rows
- apply shading to the table header row
- set the table header row to repeat across pages (if required)
- apply table borders — line styles, color, and widths
- do whatever else is required to make your table follow your corporate style.
Step 2: Save the table as a Building Block
- Select the entire table.
- Go to the Insert tab > Quick Parts.
- Select Save selection to Quick Part gallery.
- In the Create New Building Block dialog box, give the selected table a unique name (e.g. corporate_table).
- Change the Save in location from the default Building Blocks.dotx to your template (e.g. corporate_template.dotm). If your template is not listed, then you didn’t create the table in your template — start again at Step 1: Set up the table. Changing the saved location is CRITICAL as you want to share this table with all users of your template; as far as I know, Building Blocks.dotx is a local file on each person’s local machine and it’s not easy to transfer building block entries from one machine to another.
- Click OK.
- Delete the table from your template, if required.
- Save the template but don’t close it.
Step 3: Create a macro that inserts the table and its caption
- Go to the Developer tab. (Here’s how to turn it on in Word 2007 and Word 2010.)
- Click Macros.
- Type Table in the Macro Name field at the top of the Macros window.
- Click the drop-down box for Macros in and change the setting to your template (e.g. corporate_template.dotm (template)).
- Click Create.
- Copy the code in the yellow box below — copy it all; some is not displayed fully, so make sure you drag your cursor over the entire set of code when you copy it.
- In between Sub Table() and End Sub, paste the code you just copied.
- Make any changes required. You’ll need to change the name of the BuildingBlockEntries from “corporate_table” to the name of your Building Block. Other suggested changes are listed below the macro.
- Close the Visual Basic window and save the template (don’t close it yet).
' Insert Table from Building Blocks in template, insert auto caption with chapter numbering
'
'
With CaptionLabels("Table")
.NumberStyle = wdCaptionNumberStyleArabic
.IncludeChapterNumber = True
.ChapterStyleLevel = 1
.Separator = wdSeparatorHyphen
End With
Selection.InsertCaption Label:="Table", TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionAbove, ExcludeLabel:=0
Selection.TypeText Text:=" <Table Title>"
Selection.TypeParagraph
ActiveDocument.AttachedTemplate.BuildingBlockEntries("corporate_table" _
).Insert Where:=Selection.Range, RichText:=True
Other changes you might want to consider:
- This code has a hyphen (wdSeparatorHyphen) as the separator between the chapter number and the sequential table number — you can change it to something else. These options are available: wdSeparatorColon, wdSeparatorEmDash, wdSeparatorEnDash, and wdSeparatorPeriod.
- When the caption is inserted, it automatically adds three spaces after the table number and inserts <Table Title>, which is selected ready for the author to change it — if you don’t want the three spaces, or the text, change them within the double quote marks on the Selection.TypeText Text:=” <Table Title>” line.
Step 4: Assign the macro to a key combination
To make it easier for your template’s users, assign this macro to a key combination so they can just press a couple of keys to get a formatted table with its caption inserted every time.
- Open the relevant Customize window:
- Word 2007: Office button > Word Options > Customize (or click the little drop-down arrow to the far right of the Quick Access Toolbar)
- Word 2010: File > Options > Customize Ribbon.
- At the bottom of the left panel, click the Customize button.
- Scroll down the Categories list (on the left) to the end, then select Macros.
- In the list of macros on the right, select Table.
- Click in the Press new shortcut key field.
- Press the keys you want to assign to this macro — e.g. Alt+Shift+T.
- Click the drop-down box next to the Save changes in field, then select your template.
- Click Assign.
- Click Close.
- Click OK to close the Customize window.
- Save your template, but don’t close it yet.
Step 5: Test!
The final step is to test that the key combination invokes the macro and that the table and its caption is inserted as you expect.
- Go to any blank space in the template after Heading 1, then press the key combination you assigned earlier (e.g. Alt+Shift+T) — the formatted table and its automated caption should be inserted correctly.
- Be daring — try another one!
- Now delete those tables you just added and save and close the template.
- Next, open a new document based on the template and test out the key combination there too.
- Distribute your template to your authors and let them know how to insert a table the quick way using the key combination you assigned.
[Links last checked August 2011]