简体   繁体   English

使用VBA /宏将Word表中的某些单元格右对齐

[英]Align certain cells in Word table to the right with VBA/Macro

very new to VBA but our clients want the all the data in 1,850 pages of Word Tables aligned right. 对VBA来说是非常新的东西,但我们的客户希望将1,850页Word Tables中的所有数据正确对齐。 I'm thinking this is pretty easy in VBA. 我认为这在VBA中非常容易。 I am trying to figure it out and I'm sure I could nail it on my own, but a deadline is forcing me to seek help. 我正在尝试找出解决办法,我敢肯定自己会自己确定,但是最后期限迫使我寻求帮助。 So I apologize in advance if I missed a published solution. 因此,如果我错过了已发布的解决方案,请提前致歉。

As an example they want this: 例如,他们想要这样:

在此处输入图片说明

To be this: 要这样:

在此处输入图片说明

So i've got: 所以我有:

 Dim oTable As Table
    Dim oRow As Row

    For Each oTable In ActiveDocument.Tables
     For Each oRow In oTable.Rows

But I don't know how to loop through just the body of the table. 但是我不知道如何仅遍历表的主体。 Also the top 4 rows (table title) is merged to one cell and the first column is still left aligned. 同样,前4行(表标题)也合并到一个单元格中,第一列仍然保持对齐。 Help, and the next rounds on me :) 帮助,下回合对我:)

Normally I'm not a huge fan of "please write code for me" but I've not done enough with VBA in Word and want to learn some myself. 通常,我不是“请为我编写代码”的忠实拥护者,但是我对Word中的VBA不够了解,并且想学习一些自己的知识。

This is going to get you most of the way there. 这将为您提供大部分帮助。

You do not currently provide enough information to allow me to guarantee the if statement is workable for the entire document but you should be able to go from here. 您目前没有提供足够的信息,无法保证if语句对整个文档都适用,但是您应该可以从这里开始。


Sub alignTableElementsRight()
   Dim oTable As Table
   Dim oRow As Row

   Dim i As Integer
   Dim dataTable As Boolean

   For Each oTable In ActiveDocument.Tables
    'this will be set once you are in the "table" part and
    'not headings
    dataTable = False

    For Each oRow In oTable.Rows

       'you will need custom logic here to determine what your if statement
       'is to properly execute on the right row, this is going to depend based on your table
       'format, etc. This checks if a leftmost column heading is "65 to 66"
       If (InStr(oRow.Cells(1).Range.Text, "65 to 66") > 0) Then
         dataTable = True
       End If

       'if you are in the datatable, move all values to align right in each row following
       If (dataTable = True) Then
           For i = 2 To oRow.Cells.Count
               oRow.Cells(i).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
           Next i
       End If


    Next oRow

   Next oTable
End Sub

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

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