|
- Sub ImportWordTable()
- Dim wdDoc As Object
- Dim wdFileName As Variant
- Dim TableNo As Integer
- Dim iRow As Long
- Dim iCol As Integer
- Dim lastrow As Long
- lastrow = Cells(Rows.Count, 1).End(xlUp).Row
- wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
- "Browse for file containing table to be imported")
- If wdFileName = False Then Exit Sub
- Set wdDoc = GetObject(wdFileName)
- With wdDoc
- TableNo = wdDoc.tables.Count
- If TableNo = 0 Then
- MsgBox "文件沒有表格", _
- 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)
- If lastrow > 1 Then
- For iRow = 2 To .Rows.Count
- For iCol = 1 To .Columns.Count
- Cells(lastrow + iRow - 1, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
- Next iCol
- Next iRow
- Else
- 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 If
- End With
- End With
- Set wdDoc = Nothing
- End Sub
复制代码 |
|