2015-12-18 28 views
0

我想从多个word文档中提取数据(word表格第二列的信息)并将数据编译为一个excel(2011 mac版本)。 下面是例子: the word table如何使用VBA从Word表格中提取数据以优化2011?

the excel

我写代码,但是这个代码不工作。如何修改此代码以使其可以正常工作?非常感谢!

Sub extractdata() 
Dim r As Integer 
Dim c As Integer 
r = 1 
c = 8 
Range(Cells(r + 1, ”A”), Cells(65536, c)).ClearContents 
Application.ScreenUpdating = False 
Dim filename As String, wdapp As Object, erow As Long, fn As String, arr As Variant 
Set wordapp = CreateObject("word.application") 
filename = Dir(ThisWorkbook.Path & “ \ " & " * .docx”) 
Do While filename <> “” 
     With wordapp.documents.Open(ThisWorkbook.Path & "\" & filename) 
      For i = 1 To 8 
      arr = Left(.Table(1).Cells(i, 2)) 
      Next 
      Cells(erow, “A”).Resize(UBound(arr, 1), 8) = arr 
     End With 
    filename = Dir 
Loop 
Application.ScreenUpdating = True 

End Sub 
+0

发生的它,而不是工作是什么? –

+0

您可以查看http://stackoverflow.com/questions/12138958/vba-copy-from-word-table-to-excel,然后删除不必要的第一列。 – justkrys

+0

它显示“编译错误:无效或不合格的引用”,并在我的代码中“.docx”突出显示。 – user284602

回答

0

未经测试:

Sub extractdata() 

    Dim c As Long 

    c = 8 
    Range(Cells(r + 1, "A"), Cells(65536, c)).ClearContents 
    Application.ScreenUpdating = False 

    Dim filename As String, wdapp As Object, erow As Long, _ 
    Dim fn As String, i as Long 

    Set wordapp = CreateObject("word.application") 

    filename = Dir(ThisWorkbook.Path & "\*.docx") 
    Do While filename <> "" 

     With wordapp.documents.Open(ThisWorkbook.Path & "\" & filename) 
      For i = 1 To c 
       Cells(erow, i) = Left(.Tables(1).Cell(i, 2)).Range.Text 
      Next 
      .Close False 
     End With 

     filename = Dir() 
    Loop 
    Application.ScreenUpdating = True 

End Sub 
相关问题