7

I have a document in MS Word 2016 with 200+ pages and even more tables. I need to align all tables on odd pages to the left and I need all tables on even pages aligned to the right. Except for one or two tables which I can amend manually if needed none of the tables span multiple pages. Using

Dim oTable As Table
For Each oTable In ActiveDocument.Tables
    oTable.Rows.Alignment = wdAlignParagraphRight
Next oTable

I can align all tables to the right. When using wdAlignParagraphLeft instead of wdAlignParagraphRight I can align all tables to the left. But I could not figure our how to get the page number of a table so that I can assign the alignment based on the page number a table is on.

(The idea is that if printed as a book, the table are always on the inner side. If there is a better way to accomplish that I'll be listening. If printed as a book and two pages are next to each other the tables should be at the inner side like this:)

+-------------------------------+
|    Even Page  |   Odd page    |
+---------------+---------------+
|       |Table| | |Table|       |
|               |               |
+-------------------------------+

1 Answer 1

8
+200

Yankee, You can determine the page number that a particular table sits on using:

oTable.Range.Information(wdActiveEndPageNumber)

Therefor, to loop through all of the tables in your document and align them according to the odd or even nature of the page upon which they sit, you would use:

Dim oTable As Table
Dim PageNo As Integer

For Each oTable In ActiveDocument.Tables
    PageNo = oTable.Range.Information(wdActiveEndPageNumber)
    If PageNo Mod 2 = 0 Then    'The page number is EVEN.
        oTable.Rows.Alignment = wdAlignParagraphRight
    Else                        'The page number is ODD.
        oTable.Rows.Alignment = wdAlignParagraphLeft
    End If
Next oTable

Should you have any other questions, don't hesitate to ask.

1
  • 1
    If a table has page breaks and you need to get a page where the table starts, you can call tableRange.Collapse 1 before tableRange.Information(...)
    – Winand
    Commented Oct 27, 2022 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.