VBA Word: Add a Table to a Document
In this article
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
.docmif you want to keep the macro, since.docxcannot 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
- 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.
- 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.
- 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
TheRangeargument is the insertion point.Selection.Rangeuses the cursor;doc.Range(0,0)inserts at the very start;doc.Contentappends at the end. - 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.
- Capture the table in a variable. Change the line to
Dim tbl As TablethenSet tbl = doc.Tables.Add(...). Without this reference you have to hunt the table down throughdoc.Tables(1), which breaks as soon as the document has more than one table. - 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.Textreplaces the cell contents cleanly, so you do not need to strip it. - 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 throughdoc.Stylesor pick the name from the Table Styles gallery to confirm it. - Format widths and headers. Set
tbl.Rows(1).HeadingFormat = Trueto repeat the header row across pages, andtbl.Columns(1).PreferredWidthType = wdPreferredWidthPointswithPreferredWidth = 120for a fixed first column. Usetbl.AutoFitBehavior wdAutoFitWindowto span the text column. - Add rows later. Call
tbl.Rows.Addto append a row, ortbl.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. - Save as a macro-enabled file. Use File > Save As and choose Word Macro-Enabled Document (
.docm). Saving as.docxstrips 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.