如果有合并行,则无法设置 table 标题
Failing to set table heading if there are merged rows
我想将 ms-Word 文件中的所有 table 设置为每页显示一个重复标题。
但最后我发现如果 table(在下面的代码中索引为 idx)合并了行,那么
ActiveDocument.Tables(idx).Rows(1).HeadingFormat = True
失败并说
method "item" of object "rows" failed
有什么提示吗?谢谢
您可以通过选择要重复的单元格(行)来解决这个问题 - how/why 它在用户界面中有效。当然,棘手的部分是了解 table 的 "shape" 以便正确进行选择。
如果您不知道这是怎么回事,也许最好的方法是使用错误处理,这样宏就可以 "prompt" 您给出组成重复行的单元格的数量。这是一种解决方法。
(注意:除了显示某种消息外,无法暂停宏的执行。如果您想等到用户可以在 table 中实际做出选择,那么宏需要重新启动。)
Sub SetHeaderRepeatWithMergedRows()
Dim tbl As word.Table
Dim errNr As Long
Dim nrCells As Variant
Dim startCell As word.Range
Dim endCell As word.Range
'Cannot access individual rows in this collection because
'table has vertically merged cells
errNr = 5991 'or? method "item" of object "rows" failed
nrCells = "none"
On Error GoTo handler
For Each tbl In ActiveDocument.Tables
tbl.Rows(1).HeadingFormat = True
Next
Exit Sub
handler:
Select Case Err.Number
Case errNr
'Rows with vertically merged cells can only be set
'as Header Rows on the Selection, so select the
'necessary number of cells by prompting the user
Do
'Make sure the table is visible to the user:
tbl.Range.Document.ActiveWindow.ScrollIntoView tbl.Range, True
nrCells = InputBox("How many cells make up the table header:")
If IsNumeric(nrCells) Then
tbl.Cell(1, 1).Range.Select
Selection.MoveEnd wdCell, nrCells - 1
Selection.Rows.HeadingFormat = True
End If
Loop Until IsNumeric(nrCells)
nrCells = "none"
Resume Next
Case Else
MsgBox Err.Number & vbCr & Err.Description & _
vbCr & "The macro will end, now"
End Select
End Sub
我想将 ms-Word 文件中的所有 table 设置为每页显示一个重复标题。
但最后我发现如果 table(在下面的代码中索引为 idx)合并了行,那么
ActiveDocument.Tables(idx).Rows(1).HeadingFormat = True
失败并说
method "item" of object "rows" failed
有什么提示吗?谢谢
您可以通过选择要重复的单元格(行)来解决这个问题 - how/why 它在用户界面中有效。当然,棘手的部分是了解 table 的 "shape" 以便正确进行选择。
如果您不知道这是怎么回事,也许最好的方法是使用错误处理,这样宏就可以 "prompt" 您给出组成重复行的单元格的数量。这是一种解决方法。
(注意:除了显示某种消息外,无法暂停宏的执行。如果您想等到用户可以在 table 中实际做出选择,那么宏需要重新启动。)
Sub SetHeaderRepeatWithMergedRows()
Dim tbl As word.Table
Dim errNr As Long
Dim nrCells As Variant
Dim startCell As word.Range
Dim endCell As word.Range
'Cannot access individual rows in this collection because
'table has vertically merged cells
errNr = 5991 'or? method "item" of object "rows" failed
nrCells = "none"
On Error GoTo handler
For Each tbl In ActiveDocument.Tables
tbl.Rows(1).HeadingFormat = True
Next
Exit Sub
handler:
Select Case Err.Number
Case errNr
'Rows with vertically merged cells can only be set
'as Header Rows on the Selection, so select the
'necessary number of cells by prompting the user
Do
'Make sure the table is visible to the user:
tbl.Range.Document.ActiveWindow.ScrollIntoView tbl.Range, True
nrCells = InputBox("How many cells make up the table header:")
If IsNumeric(nrCells) Then
tbl.Cell(1, 1).Range.Select
Selection.MoveEnd wdCell, nrCells - 1
Selection.Rows.HeadingFormat = True
End If
Loop Until IsNumeric(nrCells)
nrCells = "none"
Resume Next
Case Else
MsgBox Err.Number & vbCr & Err.Description & _
vbCr & "The macro will end, now"
End Select
End Sub