[英]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.