简体   繁体   English

如何确定MS Word宏中表格的页码?

[英]How can I determine the page number of a table in MS Word Macro?

I have a document in MS Word 2016 with 200+ pages and even more tables. 我在MS Word 2016中有一个包含200多个页面甚至更多表格的文档。 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. 当使用wdAlignParagraphLeft而不是wdAlignParagraphRight我可以将所有表格向左对齐。 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|       |
|               |               |
+-------------------------------+

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. 如有其他疑问,请随时提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM