Word: Macro to insert a formatted table and an automated caption

August 10, 2011

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.


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.).


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. UPDATE 2016: You *can* get the caption inserted with a Quick Table — you just have to select the table AND its caption when creating it and the caption is added automatically (and adjusts the table numbering) when you insert the Quick Table. This solution may be better for those who don’t want to fiddle with macros and code — it’s very much quicker too. If you go that route, you don’t need to read any further.

Step 1: Set up the table

  1. Open your Word template — the template itself, NOT a document based on the template.
  2. 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

  1. Select the entire table.
  2. Go to the Insert tab > Quick Parts.
  3. Select Save selection to Quick Part gallery.
  4. In the Create New Building Block dialog box, give the selected table a unique name (e.g. corporate_table).
  5. 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.
  6. Click OK.
  7. Delete the table from your template, if required.
  8. Save the template but don’t close it.

Step 3: Create a macro that inserts the table and its caption

  1. Go to the Developer tab. (Here’s how to turn it on in Word 2007 and Word 2010.)
  2. Click Macros.
  3. Type Table in the Macro Name field at the top of the Macros window.
  4. Click the drop-down box for Macros in and change the setting to your template (e.g. corporate_template.dotm (template)).
  5. Click Create.
  6. 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.
  7. In between Sub Table() and End Sub, paste the code you just copied.
  8. 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.
  9. 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>"

    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.

  1. 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.
  2. At the bottom of the left panel, click the Customize button.
  3. Scroll down the Categories list (on the left) to the end, then select Macros.
  4. In the list of macros on the right, select Table.
  5. Click in the Press new shortcut key field.
  6. Press the keys you want to assign to this macro — e.g. Alt+Shift+T.
  7. Click the drop-down box next to the Save changes in field, then select your template.
  8. Click Assign.
  9. Click Close.
  10. Click OK to close the Customize window.
  11. 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.

  1. 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.
  2. Be daring — try another one!
  3. Now delete those tables you just added and save and close the template.
  4. Next, open a new document based on the template and test out the key combination there too.
  5. 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]


  1. Love this.. but, the code is cut off in the highlighted window… it works.. EXCEPT.. when it adds my table, it deletes my button (I added the Macro to a form button to execute- instead of a menu item)..PLEASE HELP

  2. Hi Huuvola

    Yes, some of the code is not displayed, which is why I ask you to copy the entire code in the yellow box at step 6.

    I’ve never done this sort of thing with a button, so I can’t help you with that. Try asking your question about the button on the Microsoft Word forum: http://answers.microsoft.com/en-us/office/forum/word — someone there may be able to help you.


  3. I think if I could scroll though the code I’d be able to repair it.. For some reason I can’t get the code in there to scroll for me.. are you able to repost or send to me privately so I can plagiarize your work more thoroughly?! Fantastic response time BTW! AWESOME!

  4. You can copy the code from here and paste it into Notepad to see it in full and see what it’s doing. Or paste it into the VBA window.


  5. hi, thanks for this. any idea which bit of code to remove so it doesn’t come up with the caption each time?

    I love your work, but never want captions, I insert them manually

  6. Hi Andy

    Try putting in the Sub bits, then just the last two lines of the macro. I haven’t tried it, but it should work as all the rest is related to the auto numbered captioning.


  7. Thank You Thank You Thank You

  8. Hi Rhonda. I’m trying to save a table as a Quick part as per your instructions above but it does not give me the option to save to the actual template. I HAVE created the table in this template. Are you able to help at all?



  9. Hi Gina

    I just tried this in Word 2010, and all I did were the main Steps 1 and 2 (not the macro stuff), and it all worked fine. The critical things are opening the template, not a doc based on the template (check the title bar — it should have the name of your template followed by the dotx or dotm extension), and sub step 5 in the Step 2 section. Make sure you give the table a name too, otherwise you can’t click the OK button.


  10. Hi there,

    This VBA code is really useful!
    however, I would like to have just the table without the header containing chapter number and all other details.
    I tried to modify the code myself, but I cannot make it to work…

    Could you please confirm how I should modify the VBA code in order to remove the header?

    Many thanks in advance!


  11. Hi Gabriele

    If by header you mean the caption, then you should be able to remove everything from the first ‘with’ to the end of ‘type.paragraph’, just leaving the comment code at the top and the code from ‘active.document’ onwards.


  12. Hi Rhonda
    I would like to have the caption below my table, but changing the code to this doesn’t do it.

    Selection.InsertCaption Label:=”Figure”, TitleAutoText:=””, Title:=””, _
    Position:=wdCaptionPositionBelow, ExcludeLabel:=0
    Selection.TypeText Text:=” ”

  13. Hi Noleen

    You’ve changed “Table” to “Figure” in the first line of your code above — have you also changed the With CaptionLabels(“Table”) line to With CaptionLabels(“Figure”)? Could that be the reason?


  14. Hi again Noleen

    I’ve also added an ‘Update’ in the ‘Solution’ section above — this may solve you problem and you won’t have to create the macro at all.


  15. Hi Rhonda
    Thank you so much for taking the time to help me. I am going to use the quick tables for simplicity. But I learnt so much from you.

  16. Maybe you could help me wth the following too? I have created a form to capture information, which I want to flow through into my document. I’ve used content controls and copied and pasted the links to the parts of my template I need. It works fine in the template, but it doesn’t work when I open a document based on the template. How can I fix this?

  17. I’ve done very little with this sort of stuff, so can’t offer any suggestions except to ask your questions on the Microsoft forums for Word. Someone there may be able to help.


  18. The code appears to be cut off below the last statement:

    ActiveDocument.AttachedTemplate.BuildingBlockEntries(“corporate_table” _
    ).Insert Where:=Selection.Range, RichText:=True

  19. I once used a functional requirements document template that would automatically number the table rows to correspond with the heading number. So if the (level 2) heading was 6.1 The rows in the table would be FR1-001, FR1-002 etc. If the heading was 6.2 The rows in the table would be FR2-001, FR2-002 etc. and you could just copy and paste the heading and table and it would automatically adjust the numbering. Do you have any instructions for how this was done?

  20. Hi Sonja

    That one’s way outside my level of expertise — sorry!

    Consider asking your question on the Microsoft Word forum.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: