[英]Align certain cells in Word table to the right with VBA/Macro
对VBA来说是非常新的东西,但我们的客户希望将1,850页Word Tables中的所有数据正确对齐。 我认为这在VBA中非常容易。 我正在尝试找出解决办法,我敢肯定自己会自己确定,但是最后期限迫使我寻求帮助。 因此,如果我错过了已发布的解决方案,请提前致歉。
例如,他们想要这样:
要这样:
所以我有:
Dim oTable As Table
Dim oRow As Row
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
但是我不知道如何仅遍历表的主体。 同样,前4行(表标题)也合并到一个单元格中,第一列仍然保持对齐。 帮助,下回合对我:)
通常,我不是“请为我编写代码”的忠实拥护者,但是我对Word中的VBA不够了解,并且想学习一些自己的知识。
这将为您提供大部分帮助。
您目前没有提供足够的信息,无法保证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.