查找修订并报告修订发生的步骤编号

时间:2015-05-26 21:23:01

标签: vba word-vba

我正在编写一个可以在Word文档中找到修订版的代码,并在一个单独的表中(已经设置并且可以使用)报告列表中的修订版本和编号(步骤编号)。 (见附图)。因此,例如,第3行中的任何修订都将报告步骤编号10.1。所以我想的是,对于每个修订版,它将转到步骤编号所在行的HOME单元格(然后如果没有,则向上移动以找到一个)。当没有任何修订时,这很好用,我只是把光标放在随机单元格中。我认为我的代码出了什么问题是它没有将光标放在修订版本上,所以让代码将光标移动到起始行并没有做任何事情。 我试过改变 stepnumber = oRevision.Range.Paragraphs(1).Range.ListFormat.ListString 至 stepnumber = oRevision.Range.Rows(1).Range.ListFormat.ListString 但得到了垂直合并单元格的错误。 如何获取表格中任何单元格的修订文本和步骤编号?

Dim oRange As Range
Dim oRevision As Revision
Dim strText As String
Dim n As Long
Dim i As Long

For Each oRevision In oDoc.Revisions
    Select Case oRevision.Type
        'Only include insertions and deletions
        Case wdRevisionInsert, wdRevisionDelete
            'In case of footnote/endnote references (appear as Chr(2)),
            'insert "[footnote reference]"/"[endnote reference]"
            With oRevision
                'Get the changed text
                strText = .Range.Text

stepnumber = oRevision.Range.Paragraphs(1).Range.ListFormat.ListString

If oRevision.Range.Rows(1).Range.ListFormat.ListString = "" Then
oRevision.Range.Select
Selection.HomeKey Unit:=wdRow
stepnumber = oRevision.Range.Paragraphs(1).Range.ListFormat.ListString

ElseIf oRevision.Range.Paragraphs(1).Range.ListFormat.ListString = "" Then
Do
Selection.MoveUp Unit:=wdLine, Count:=1
Loop Until oRevision.Range.Paragraphs(1).Range.ListFormat.ListString <> ""
stepnumber = oRevision.Range.Paragraphs(1).Range.ListFormat.ListString
End If

                End With
            'Add 1 to counter
            n = n + 1
            'Add row to table
            Set oRow = oTable.Rows.Add

            'Insert data in cells in oRow
            With oRow


                'SECTION
                    .Cells(1).Range.Text = _
                    "Step " & stepnumber

                'Type of revision
                If oRevision.Type = wdRevisionInsert Then
                    .Cells(2).Range.Text = "Inserted: " & strText
                    .Range.Font.Bold = False

                Else
                    .Cells(2).Range.Text = "Deleted: " & strText
                    .Range.Font.Bold = False
                End If

            End With
    End Select
Next oRevision

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用Table.Cell(r, c)方法绕过合并的单元格问题:

Sub Tester()

    Dim rng As Range, rev As Revision, c As Cell
    Dim rownum As Long

    For Each rev In ThisDocument.Revisions

        Set rng = rev.Range
        rng.Collapse direction:=wdCollapseStart

        If rng.Information(wdWithInTable) Then

            rownum = rng.Information(wdEndOfRangeRowNumber)

            Set c = rng.Tables(1).Cell(rownum, 1) 'first cell on the row

            Debug.Print c.Range.Paragraphs(1).Range.ListFormat.ListString

        End If
    Next rev

End Sub
相关问题