宏将MS Word表导出到Excel工作表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了宏将MS Word表导出到Excel工作表相关的知识,希望对你有一定的参考价值。

我有一个包含许多表的word文档。有谁知道如何编写宏来将这些表导出到不同的Excel工作表?

答案

答案取自: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),然后将代码粘贴到代码窗格中。您可以像使用任何其他(Alt-TMM)一样从Excel界面运行宏。

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


保持优秀。

达蒙

VBAexpert Excel Consulting(我的其他人生:http://damonostrander.com

另一答案

喜欢它,这绝对是辉煌的,达蒙(即使我花了一年多才找到......)。这是我的最终代码,添加循环遍历所有表(从所选表开始):

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

另一答案

这部分代码是循环遍历每个表并将其复制到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
另一答案

非常感谢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

以上是关于宏将MS Word表导出到Excel工作表的主要内容,如果未能解决你的问题,请参考以下文章

使用 VBA 将 MS Access 记录集导出到 Excel 中的多个工作表/选项卡会生成只读文件

将值导出到 Excel MS Access

VBA 把Excel的内容复制到Word的代码?

将数据从 MS Sql Server 存储过程导出到 excel 文件

MS SQL SERVER导出表结构到Excel

jsp编写网站中,如何将数据库中的表导出到txt或者word,excel