选择要从Word文件复制到Excel文件的表

问题描述 投票:0回答:1

我有一个包含7个表的Word文件,我想将其中一个复制到Excel文件中。但是我想每次运行宏时都选择要复制的表,因为我将拥有不同的Word文件,其中包含不同数量的表和要复制的不同表。

现在,我有这段代码可以复制您此时选择的单词表:

    Dim wrdTbl As Table
    Dim RowCount As Long, ColCount As Long, i As Long, j As Long
    Dim oXLApp As Object, oXLwb As Object, oXLws As Object

    Set wrdTbl = Selection.Tables(1)
    ColCount = wrdTbl.Columns.Count
    RowCount = wrdTbl.Rows.Count

    Set oXLApp = CreateObject("Excel.Application")

    oXLApp.Visible = False

    Set oXLwb = oXLApp.Workbooks.Open("C:\Sample.xlsx")
    Set oXLws = oXLwb.Sheets(1)

    For i = 1 To RowCount
        For j = 1 To ColCount
            Debug.Print wrdTbl.Cell(i, j).Range.Text

           With oXLws
                .Cells(1, 1).Value = wrdTbl.Cell(i, j).Range.Text
            End With
        Next
    Next
    oXLwb.Close savechanges:=True
    Set oXLws = Nothing
    Set oXLwb = Nothing
    oXLApp.Quit
    Set oXLApp = Nothing

    MsgBox "DONE"
End Sub
excel vba ms-word copy-paste word-table
1个回答
0
投票
Set wrdTbl = Selection.Tables(1)

with:

Set wrdTbl = ActiveDocument.Tables(InputBox("Table # to copy? There are " & ActiveDocument.Tables.Count & " tables to choose from."))
© www.soinside.com 2019 - 2024. All rights reserved.