简体   繁体   English

如何使用VBA从单词表中提取数据到excel 2011?

[英]How to extract data from word tables to excel 2011 using VBA?

I'm trying to extract data (the information on the second column of word tables) from multiple word documents and compile the data to one excel (2011 mac version). 我正在尝试从多个Word文档中提取数据(单词表第二列中的信息),并将数据编译为一个Excel(2011 Mac版本)。 Below is the example: the word table 下面是示例: 单词表

the excel 卓越

I wrote a code, but this code doesn't work. 我写了一个代码,但是这个代码不起作用。 How to revise this code so that it can work properly? 如何修改此代码以使其正常工作? thanks a lot! 非常感谢!

    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

Untested: 未经测试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM