2013-09-23 4 views
1

모든, 나는 각각의 모든 테이블을 추출 그러나 다만 특정 폴더에있는 각 단어의 문서의 첫 번째 테이블을 잡아되지 아래의 코드를 수정하는 방법(여러 문서)

문서? 내가 직접 코드를 조작하려고 시도했지만 올바르게 할 수없는 것처럼 보입니다. 어떤 도움이라도 대단히 감사하겠습니다.

Option Explicit 

Sub test() 

Dim oWord As Word.Application 
Dim oDoc As Word.Document 
Dim oCell As Word.Cell 
Dim sPath As String 
Dim sFile As String 
Dim r As Long 
Dim c As Long 
Dim Cnt As Long 

Application.ScreenUpdating = False 

Set oWord = CreateObject("Word.Application") 

sPath = "C:\Users\Domenic\Desktop\" 'change the path accordingly 

If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 

sFile = Dir(sPath & "*.doc") 

r = 2 'starting row 
c = 1 'starting column 
Cnt = 0 
Do While Len(sFile) > 0 
Cnt = Cnt + 1 
Set oDoc = oWord.Documents.Open(sPath & sFile) 
For Each oCell In oDoc.Tables(1).Range.Cells 
    Cells(r, c).Value = Replace(oCell.Range.Text, Chr(13) & Chr(7), "") 
    c = c + 1 
Next oCell 
oDoc.Close savechanges:=False 
r = r + 1 
c = 1 
sFile = Dir 
Loop 

Application.ScreenUpdating = True 

If Cnt = 0 Then 
    MsgBox "No Word documents were found...", vbExclamation 
End If 

End Sub 

답변

1
Dim tbl 

'........ 
Set oDoc = oWord.Documents.Open(sPath & sFile) 
For each tbl in oDoc.Tables 
    For Each oCell In tbl.Range.Cells 
     Cells(r, c).Value = Replace(oCell.Range.Text, Chr(13) & Chr(7), "") 
     c = c + 1 
    Next oCell 
    r = r + 2 'couple of blank rows between tables 
    c = 1 
Next tbl 

oDoc.Close savechanges:=False 
'.........