다른 토론에서 Word에서 Excel로 표를 가져 오는이 매크로를 찾을 수있었습니다.Excel VBA에서 Word 표 서식 유지
잘 작동하지만 어떻게 Word 표의 형식을 유지할 수 있습니까?
몇 가지 방법을 시도했지만 제대로 작동하지 않습니다. 한 번에 1 개가 아닌 여러 파일을 동시에 수행 할 수있는 방법이 있습니까?
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
이것은 굉장합니다. 감사. 하지만 한 가지 문제가 있습니다. 이것은 처음 두 테이블을 엉망으로 만듭니다. 첫 번째 테이블 (2 열)의 형식을 취하고 두 번째 테이블의 첫 번째 2 열을 붙여 넣습니다. 그 후 괜찮습니다. 이 문제를 어떻게 해결할 수 있습니까? – Nolemonkey