Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Word: Add a Table to a Document

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You add a table to a Word document from VBA with the Tables.Add method on a Range or Selection object, for example ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4. That single line inserts a real Word table at the current cursor position, and you can then fill cells through the Cell object. The steps below cover the object model, a working macro, formatting, and the errors that trip people up most often.

What You'll Need

  • Microsoft Word for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Word > Preferences > Ribbon & Toolbar on Mac).
  • A document open and saved as .docm if you want to keep the macro, since .docx cannot store VBA.
  • Basic familiarity with the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on Mac).
  • Macro security set to allow macros in trusted locations; otherwise Word blocks the code silently.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). Menu labels and dialog wording can differ slightly by Word version and region, but the editor layout is consistent.
  2. Insert a module. In the Project pane, right-click your document's VBAProject and choose Insert > Module. Code in a module is easier to run and debug than code behind a document.
  3. Write the basic Add call. Type a procedure such as:
    Sub AddTable()
      Dim doc As Document
      Set doc = ActiveDocument
      doc.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4
    End Sub

    The Range argument is the insertion point. Selection.Range uses the cursor; doc.Range(0,0) inserts at the very start; doc.Content appends at the end.
  4. Run it. Press F5 with the cursor inside the procedure, or place the cursor in the document where you want the table and run the macro from the Macros dialog (Alt+F8). The table appears immediately.
  5. Capture the table in a variable. Change the line to Dim tbl As Table then Set tbl = doc.Tables.Add(...). Without this reference you have to hunt the table down through doc.Tables(1), which breaks as soon as the document has more than one table.
  6. Fill the cells. Use tbl.Cell(1,1).Range.Text = "Item" and repeat for each cell. Note that Word appends an end-of-cell marker; assigning .Text replaces the cell contents cleanly, so you do not need to strip it.
  7. Apply a built-in style. Set tbl.Style = "Grid Table 4 - Accent 1". Style names must match a table style installed in the document; if the name is wrong Word raises error 5941. Loop through doc.Styles or pick the name from the Table Styles gallery to confirm it.
  8. Format widths and headers. Set tbl.Rows(1).HeadingFormat = True to repeat the header row across pages, and tbl.Columns(1).PreferredWidthType = wdPreferredWidthPoints with PreferredWidth = 120 for a fixed first column. Use tbl.AutoFitBehavior wdAutoFitWindow to span the text column.
  9. Add rows later. Call tbl.Rows.Add to append a row, or tbl.Rows.Add BeforeRow:=tbl.Rows(2) to insert above a specific row. Adding rows is how you build a table of unknown length from a loop over data.
  10. Save as a macro-enabled file. Use File > Save As and choose Word Macro-Enabled Document (.docm). Saving as .docx strips the code with a warning.

Common Mistakes

Passing a collapsed range. Tables.Add needs a range; if you pass Nothing or a range from a different document, Word throws error 4605 or 5941. Always qualify with the document you are inserting into.

Assuming Tables(1) is your table. In a document with existing tables, index 1 may be an unrelated table. Keep the object reference returned by Add.

Using Selection when no document is active. If the macro runs while a dialog is open or focus is elsewhere, Selection can fail. Prefer an explicit Document and Range.

Forgetting the header row flag. Without HeadingFormat = True, long tables lose their column titles on page two, which readers notice immediately.

Hard-coding style names. Table style names vary between templates and Word versions; a name that works in one document fails in another. Wrap the assignment in error handling or verify the style exists first.

Tips

Build tables from the bottom up when inserting into existing content: add the table at doc.Content, then move it with tbl.Range.Cut and Selection.Paste only if you truly need to reposition it, because moving tables is fiddly.

Use Application.ScreenUpdating = False at the start and restore it at the end when adding many tables; it speeds up long macros noticeably.

For data-driven tables, size the table to the data first (NumRows:=dataCount + 1) rather than adding rows one at a time; it is faster and avoids repeated reflow.

Set tbl.AllowAutoFit = False before setting fixed widths, otherwise Word may resize columns as you type into cells.

Test on a copy of the document. Table insertion changes the document structure, and an early bug can leave half-built tables behind.

Troubleshooting

Nothing happens when you run the macro. Check that macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings) and that the code sits in a standard module, not in ThisDocument with a name clash.

Error 5941, "The requested member of the collection does not exist." Usually a bad table style name or a Tables(index) that is out of range. Print doc.Tables.Count in the Immediate window to confirm what exists.

Error 4605, "This method or property is not available because the current selection is not in a table." You are calling a table method on a range that is not inside a table. Re-check the range you passed to Add.

The table lands in the wrong place. Selection.Range follows the cursor; if the cursor moved, so does the table. Use an explicit range such as doc.Paragraphs(3).Range for predictable placement.

Formatting reverts. Direct formatting on cells can be overridden by the table style. Either pick a style that already matches, or set cell-level formatting after assigning the style.

If that did not work, step through the macro with F8 one line at a time. The line that fails tells you exactly which object is missing, which is faster than guessing.

Frequently Asked Questions

Can I add a table without using Selection?

Yes. Pass any Range object, such as ActiveDocument.Range(0,0) for the start of the document or ActiveDocument.Content for the end. This is more reliable in macros that run unattended.

How do I add a table at the end of the document?

Use ActiveDocument.Content as the Range argument, or move the range to ActiveDocument.Range(ActiveDocument.Content.End - 1, ActiveDocument.Content.End - 1) to avoid inserting inside the final paragraph mark.

Why does my table style assignment fail?

The style name does not exist in that document. Table styles are document- or template-specific, so copy the exact name from the Table Styles gallery or loop through ActiveDocument.Styles to list valid names.

Can I create a table from an array or Excel data?

Yes. Size the table to the data, then loop rows and columns assigning tbl.Cell(r,c).Range.Text. For large datasets, write to a string with tab separators, insert it as text, then convert with Range.ConvertToTable, which is much faster than cell-by-cell assignment.

Does this work in Word for Mac?

The object model is largely the same, but some style names and dialog paths differ, and macro security prompts behave differently. Test the macro on the target platform before distributing it.

How do I delete a table I just added?

Call tbl.Delete. Do not use tbl.Range.Delete, which removes the contents but can leave an empty table structure behind.