Word 2007/2010: Using a Macro to Automatically Format Tables in a Document

Printer-friendly versionPrinter-friendly versionSend by emailSend by emailPDF versionPDF version

If you are working with an MS Word document that contains several tables, you might need a way to automate some tasks. Tasks that can be automated includes formatting the tables in the document. For intance, the tasks that you can automate are those that are repeatitive tasks. This includes formatting text, formatting the table headers, setting text alignments, and many others.

Word macro

If the document that you are working contains several similar tables, automating some formatting tasks will prove helpful. The clue here is "similar tables". That is, if the tables in the document need to be formatted similarly, then you can automate the entire formatting tasks. This includes formatting the table headers, setting cell borders, setting text alignments, sizing columns and many others.

For more information on how to create a macro in MS Word, please use this link: http://www.technicalforum.org/ms-office/how-create-and-record-macro-ms-w....

In the example that I shown below, the tables - that is, all tables in my document will be formatted similarly provided that the tables contain five (5) columns. Here are the formatting tasks that are automated by the macro:

  1. Set all text to Calibri size 10.
  2. Resize the first column to 1.33" and background color shall be light violet.
  3. Resize the second column to 0.63".
  4. Resize the third column to 0.69".
  5. Resize the fourth column to 0.75".
  6. Resize the fifth column to 2.7".

The resulting table shall look like this:

Sample table format

In order for your 5-column table to be formatted like the table above, you need to create a macro, name it like Format_Table, then copy this code:

Sub Format_Table()
'A quick macro for formatting tables by Jess Magz
Dim oTbl As Word.Table
Dim oCell As Word.Cell
For Each oTbl In ActiveDocument.Tables
    Selection.Font.Name = "Calibri"
    Selection.Font.Size = 10
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(1.33)
    Selection.Columns.Shading.BackgroundPatternColor = -654246093
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(0.63)
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(0.69)
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(0.75)
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(2.7)
    oTbl.Rows(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    oTbl.Rows(1).Cells.VerticalAlignment = wdCellAlignVerticalCenter
    oTbl.Rows(1).Range.Bold = True
Next oTbl
End Sub

Save the macro and then run. All the tables in the document shall be formatted automatically.

MS Office Software: