vba 在包含多段单元格的 Word 表格中向下移动一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14327072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:10:51  来源:igfitidea点击:

Moving down a row in a Word table containing multi-paragraph cells

vbams-wordword-vbaword-2010

提问by Freda K

How can I reliably move down the rows in a Word table?
Here's the structure of the table. Note that the first and second columns can both have multiple lines and paragraphs.

如何可靠地向下移动 Word 表中的行?
这是表的结构。请注意,第一列和第二列都可以有多个行和段落。

Rule ID         1

Logic           Date must be equal to or greater than 01-Jan-2012 

Discrepancy     Date is before 01-Jan-2012
message

Test case 1.1   Create form where Date is before 01-Jan-2012 
                Verify discrepancy message appears.
                Print form.

Test case 1.2   Create form where Date is equal to 01-Jan-2012.
                Verify no discrepancy message appears.
                Print form.

Test case 1.3   Create form where Date is after 01-Jan-2012.
                Verify no discrepancy message appears.
                Print form.

I have tried a number of methods to move down the table.

我尝试了多种方法来向下移动表格。

When I tried Selection.MoveDownwith unit:=wdLine(below) I ran into problems when column 1 contained a word wrap.

当我尝试Selection.MoveDown使用unit:=wdLine(下面)时,当第 1 列包含自动换行时遇到了问题。

Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdMove

When I tried Selection.MoveDownwith unit:=wdParagraph(below), I ran into problems when column 2 contained multiple paragraphs.

当我尝试Selection.MoveDown使用unit:=wdParagraph(如下)时,当第 2 列包含多个段落时,我遇到了问题。

Selection.MoveDown unit:=wdParagraph, Count:=3 

unit:=wdRowdoes not appear to be a valid parameter for Selection.MoveDown.
Selection.Cells(1).RowIndexis a read-only parameter

unit:=wdRow似乎不是 的有效参数Selection.MoveDown
Selection.Cells(1).RowIndex是只读参数

Does anyone know of a simple method to move down the table one row at a time that would handle both the word wraps in column 1 and the multiple paragraphs in column 2?

有没有人知道一种简单的方法可以一次将表格向下移动一行,以处理第 1 列中的自动换行和第 2 列中的多个段落?

回答by Robert Harvey

Try something like this. It's a generalized algorithm for looping through each row and column of all the tables in a Word document. Modify as needed (untested code):

尝试这样的事情。它是一种通用算法,用于遍历 Word 文档中所有表格的每一行和每一列。根据需要修改(未经测试的代码):

Sub ModifyAllTables()
  Dim oTbl As Table
  Dim oRow As Row
  Dim oRng As Range
  For Each oTbl In ActiveDocument.Tables
    For Each oRow In oTbl.Rows
      ' Select the cell in column 2.
      Set oRng = oRow.Cells(2).Range
      ' and do something with it...
      'e.g. oRng.TypeText "modified"
    Next
  Next
End Sub

回答by Tim Williams

Sub NextRow()

    Dim c As Long, r As Long

    With Selection
        'ignore if not in table
        If .Information(wdWithInTable) Then

            c = .Columns(1).Index
            r = .Rows(1).Index

            If .Rows(1).Parent.Rows.Count >= r + 1 Then
                .Rows(1).Parent.Rows(r + 1).Cells(c).Range.Select
            End If
        End If
    End With

End Sub