如何使用 Word VBA 宏在 header 列下方添加一行并将现有行向下移动?
How to add a row below the column header and move the existing row downwards using Word VBA Macros?
每当我更新文档时,我都需要在 table 的顶部添加一行,就在 header 的下方,然后将旧行移动到下一层。 table 通常出现在 Word 文档的最后一页。请在附件中找到预期和实际行为的屏幕截图。
下面是我为实现而编写的代码:
- 转到最后一页并找到 table
- 添加一行(这会在列 header 上方添加)
期望:
- 紧跟在列之后添加行header并将现有行移动到下一级
- 将日期添加到第二列。
Click for the screenshot
我的代码:
子宏 1()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First)
'Other row formatting
Next theTable
结束子
我正在学习 VBA,任何帮助都会很有用。谢谢你的时间。
screenshot2
例如:
With ActiveDocument
With .Tables(.Tables.Count)
.Split (2)
With .Range.Characters.Last.Next
.FormattedText = .Next.Rows(1).Range.FormattedText
End With
.Rows(2).Range.Delete
End With
End With
不过,您所说的“将现有行移动到下一层”是什么意思还不清楚。
需要下一个。
Sub test()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First.Next) '<~~ add next
'Other row formatting
Next theTable
End Sub
如 Intellisense 所示,Table.Rows.Add
的输入参数是 [BeforeRow]
。这是一个可选参数,因此如果省略它,则会在 table.
的最后一行之后添加新行
因为您希望该行出现在第一行之后和第二行之前,您需要将第二行作为参数传递,例如
With ActiveDocument.Tables(ActiveDocument.Tables.Count)
With .Rows.Add(.Rows(2))
'add date
.Cells(2).Range.Text = Format(Date, "MMMM d, yyyy")
'add any additional row formatting
End With
End With
每当我更新文档时,我都需要在 table 的顶部添加一行,就在 header 的下方,然后将旧行移动到下一层。 table 通常出现在 Word 文档的最后一页。请在附件中找到预期和实际行为的屏幕截图。 下面是我为实现而编写的代码:
- 转到最后一页并找到 table
- 添加一行(这会在列 header 上方添加)
期望:
- 紧跟在列之后添加行header并将现有行移动到下一级
- 将日期添加到第二列。 Click for the screenshot
我的代码:
子宏 1()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First)
'Other row formatting
Next theTable
结束子
我正在学习 VBA,任何帮助都会很有用。谢谢你的时间。
screenshot2
例如:
With ActiveDocument
With .Tables(.Tables.Count)
.Split (2)
With .Range.Characters.Last.Next
.FormattedText = .Next.Rows(1).Range.FormattedText
End With
.Rows(2).Range.Delete
End With
End With
不过,您所说的“将现有行移动到下一层”是什么意思还不清楚。
需要下一个。
Sub test()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First.Next) '<~~ add next
'Other row formatting
Next theTable
End Sub
如 Intellisense 所示,Table.Rows.Add
的输入参数是 [BeforeRow]
。这是一个可选参数,因此如果省略它,则会在 table.
因为您希望该行出现在第一行之后和第二行之前,您需要将第二行作为参数传递,例如
With ActiveDocument.Tables(ActiveDocument.Tables.Count)
With .Rows.Add(.Rows(2))
'add date
.Cells(2).Range.Text = Format(Date, "MMMM d, yyyy")
'add any additional row formatting
End With
End With