Insert Line Numbers in Table Rows

406 Views Asked by At

VBA for Line Numbers in Tables?

I am wondering how to use a Word Macro or VBA to provide poetry line numbers in tables.

Example of a Table

Below is illustrative of one table from this document. The "Pre" represents the two-column tables as they are. The "Post" representing the desired numbering.

PRE: EXAMPLE OF 1 OF THE TABLES IN DOCUMENT      POST EXAMPLE NUBMERING DESIRED 
                       
 ---------------------------------------         ----------------------------------------
| Lorem ipsum dolor sit   amet,     |   |       | Lorem ipsum dolor sit amet,       |    |
| consectetur adipiscing elit.      |   |       | consectetur adipiscing elit.      |    |
| Sed vel nibh et purus             |   |       | Sed vel nibh et purus             |    |
| elementum commodo.                |   |       | elementum commodo.                |    |
|                                   |   |       |                                   |    |
| Mauris sed justo a massa          |   |       | Mauris sed justo a massa          | 5  |
| lacinia condimentum               |   |       | lacinia condimentum               |    |
| lacinia eget sem.                 |   |       | lacinia eget sem.                 |    |
|                                   |   |       |                                   |    |
| Ut et nunc quis ante tincidunt    |   |       | Ut et nunc quis ante tincidunt    |    |
| porta vitae eu lorem.             |   |       | porta vitae eu lorem.             |    |
|                                   |   |       |                                   |    |
| In aliquet elit eget              |   |       | In aliquet elit eget              | 10 |
| fringilla suscipit.               |   |       | fringilla suscipit.               |    |
| Maecenas dictum turpis in         |   |       | Maecenas dictum turpis in         |    |
| lectus porttitor placerat.        |   |       | lectus porttitor placerat.        |    |
|                                   |   |       |                                   |    |
| Maecenas nec magna fringilla      |   |       | Maecenas nec magna fringilla      |    |
| ex scelerisque aliquet            |   |       | ex scelerisque aliquet            | 15 |
| ac consequat felis.               |   |       | ac consequat felis.               |    |

Existing document details:

  • One docx
  • Docx contains multiple two-column tables
  • Existing text is in the left column
  • Some rows in that left "text" column are blank lines
  • The right column could be empty or
  • Tables of varying length
  • Some tables spanning multiple pages
  • Lots of other text that is not in tables

Post-Condition Sought:

  • For all tables in the document
  • Line numbers filled in the right columns
  • Counting every fifth row, starting at 5, where the left column isn't blank
  • Placing the non-blank line count in the right column
  • Overwriting any content in the left column
  • Overwriting blanks in rows where the line count isn't a increment of 5

Things I haven't been able to Google readily

I'm a complete VBA/Macro novice so bear with me:

  • Iterate over all tables that exist in the document
  • Count text length in the left cells so I can determine if they contain text
1

There are 1 best solutions below

0
On

Microsoft's support page is specific that when line numbering is applied, a table counts as only 1 line: Add or remove line numbers So applying normal Line Numbering won't work, as I imagine you have discovered.

One relatively simple workaround would be to use a numbered list style for the content of the tables, then make the blank lines a non-numbered style. This will display a number to the left of each line instead of your preferred display.

You don't mention the row structure of the tables. If each line of the poetry is in a separate row, your programming work will be easier. This makes a difference to the code used to find if the line or row is empty.

Iterating over each table:

Sub TableFormatting()
    Dim oTable As Table
    For Each oTable In ActiveDocument.Tables
        'Do process here
    Next oTable
End Sub