如何查找和复制指定文本下方的表格

时间:2016-09-01 10:39:06

标签: vba word-vba

我有一个word文档,其中包含多个文本标签,后跟各自的表格。 我想找到一个文本然后复制它下面的表格,以便我可以将其粘贴到其他文档中。

Sub EWT()
    Dim para As Paragraph
    Dim textA As String

    For Each para In ActiveDocument.Paragraphs
        If Not para.Range.Information(wdWithInTable) Then
            textA = para.Range.Text
            Debug.Print textA
            'How to get the table just below this and copy it?
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

以下是查找表格的一般方法。简而言之,创建一个新范围,指定文本离开的起点,结束是文档的结尾。这是第一张桌子。作为补充建议,请避免使用信息功能。一个原因是它很慢。

  

Sub EWT()       Dim para As Paragraph       Dim textA As String

For Each para In ActiveDocument.Paragraphs
    If Not para.Range.Information(wdWithInTable) Then
        textA = para.Range.Text
        para.Range.Select 'Debug only

        Dim myRange As Range
        Set myRange = ActiveDocument.Range
        myRange.Start = para.Range.End
        myRange.End = ActiveDocument.Range.End

        Dim myTable As Table
        Set myTable = myRange.Tables(1)
        myTable.Select  'Debug only

        Debug.Print textA
        'How to get the table just below this and copy it?
    End If
Next End Sub

然后,看起来你想要查找文档中的每个表,因为你的查找不会查找任何特定文本,只查找文本。如果是这种情况,这会更快:

  

Sub FindTables()       将myDocument作为文档调暗       设置myDocument = ActiveDocument

Dim myTable As Table
For Each myTable In myDocument.Tables
    myTable.Select
Next End Sub