欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VBA:word表格导入excel

程序员文章站 2022-04-09 16:52:57
...
爬文爬到的:原地址https://*.com/questions/4465212/macro-to-export-ms-word-tables-to-excel-sheets
上述链接里:1.单个doc中1个table处理,2.单个doc中多个table处理,3.单个docx中的table处理。这里我只引用了程序2,感兴趣的话请直接参考原文!!!!!!
https://blog.csdn.net/u012236241/article/details/80426945 
                    配合讲word里的表格合并和在一个word里,可以实现批量处理word中的表格,转换成一个大的excel。
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              '写到excel的数据,+1就是表格间空一行
    Next tableStart
End With

End Sub
相关标签: word