将MS Word表格导出为Excel工作表的宏

29

我有一个包含许多表格的Word文档。请问有没有人知道如何编写宏来将这些表格导出到不同的Excel工作表中?

4个回答

33

以下是从http://www.mrexcel.com/forum/showthread.php?t=36875获取的答案。

这里有一些代码,可以将Word中的表格读取到Excel的活动工作表中。 如果Word包含多个表格,则它会提示您输入Word文档以及表格编号。

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    TableNo = wdDoc.tables.Count
    If TableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf TableNo > 1 Then
        TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
        "Enter table number of table to import", "Import Word Table", "1")
    End If
    With .tables(TableNo)
        'copy cell contents from Word table cells to Excel cells
        For iRow = 1 To .Rows.Count
            For iCol = 1 To .Columns.Count
                Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
            Next iCol
        Next iRow
    End With
End With

Set wdDoc = Nothing

End Sub

这个宏应该被插入到Excel中(而不是Word),并放置在标准的宏模块中,而不是工作表或工作簿事件代码模块中。要做到这一点,请进入VBA(键盘Alt-TMV),插入一个宏模块(Alt-IM),并将代码粘贴到代码窗格中。像其他宏一样从Excel界面运行宏(Alt-TMM)。

如果您的文档包含许多表格,例如,如果您的100页以上的表格实际上是每一页单独的表格,则可以轻松修改此代码以读取所有表格。但现在我希望它是一个连续的表格,并且不需要任何修改。


继续用Excel吧。

Damon

VBAexpert Excel咨询 (我另一个生活:http://damonostrander.com


谢谢你的代码。我认为我可以修改你的代码来读取所有表格,但是我该如何为每个表格创建不同的Excel工作表呢? - QLands
这些源代码不保留原始Word表格的文本格式。是否存在任何解决方案? - user606310
如果代码在解析表时抛出错误,请尝试将此代码放置在“With wdDoc”行之后的新行上:“On Error Resume Next”。这基本上意味着,如果单元格引发可恢复错误,则代码执行不会停止,而是继续执行到下一个单元格。 - Santhos
这段代码将Word中的内容导入到正在运行的Excel表格中。有没有人知道一个示例,可以将正在运行的Word文档中的表格内容导出到Excel表格中? - dolphus333
顺便提一下,由于这篇文章已经超过10年了,如果你偶然发现了这个好用的解决方案,你可以将 .doc 改为 .docx 以打开现代的 Word 文档文件扩展名。 - ChaosPH

20

我做了一个修改,通过添加循环来遍历所有表格(从选择的表格开始):这个链接

Option Explicit

Sub ImportWordTable()

Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer

On Error Resume Next

ActiveSheet.Range("A:AZ").ClearContents

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    tableNo = wdDoc.tables.Count
    tableTot = wdDoc.tables.Count
    If tableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf tableNo > 1 Then
        tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
        "Enter the table to start from", "Import Word Table", "1")
    End If

    resultRow = 4

    For tableStart = 1 To tableTot
        With .tables(tableStart)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 1 To .Rows.Count
                For iCol = 1 To .Columns.Count
                    Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                Next iCol
                resultRow = resultRow + 1
            Next iRow
        End With
        resultRow = resultRow + 1
    Next tableStart
End With

End Sub

下一个技巧:学会从Word中提取表格中的子表格...但我真的想这么做吗?
TC

非常感谢。我不得不将For tableStart = 1 To tableTot更改为For tableStart = tableNo To tableTot,以便它从您告诉它的地方开始。还对其进行了修改,使每个表都存储在单独的Excel工作簿中。 - javydreamercsw

1

非常感谢Damon和@Tim。

我修改了它以打开docx文件,在检查用户是否退出后移动了工作表清除线。

以下是最终代码:

Option Explicit

Sub ImportWordTable()

Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer      'table number in Word
Dim iRow As Long            'row index in Excel
Dim iCol As Integer         'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer

On Error Resume Next

wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

ActiveSheet.Range("A:AZ").ClearContents

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    tableNo = wdDoc.tables.Count
    tableTot = wdDoc.tables.Count
    If tableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf tableNo > 1 Then
        tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
        "Enter the table to start from", "Import Word Table", "1")
    End If

    resultRow = 4

    For tableStart = tableNo To tableTot
        With .tables(tableStart)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 1 To .Rows.Count
                For iCol = 1 To .Columns.Count
                    Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                Next iCol
                resultRow = resultRow + 1
            Next iRow
        End With
        resultRow = resultRow + 1
    Next tableStart
End With

End Sub

0
这一段代码是循环遍历每个表格并将其复制到Excel中的部分。也许您可以创建一个工作表对象,使用表格编号作为计数器动态更新所引用的工作表。
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接