仅供初学者使用,我的VBA体验有限,而且我主要修改我在网上发布的内容。我有一个Excel宏,可以从Word表格中的表格(或多个表格)中获取数据。我的问题是,我拥有一千个Word文档,所以我希望能找到一个解决方案,帮助您从用户选择的文件夹中的所有Word文档复制数据。打开多个Word文档
这里是我当前的代码:
Sub ImportWordTables()
'Imports cells from Word document Tables in multiple documents
Dim wdDoc As Object
Dim TableNo As Integer 'number of tables in Word doc
Dim iTable As Integer 'table number index
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim ix As Long
ix = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
LastRow = ix
wdFileName = Application.GetOpenFilename("Word files (*.doc*),*.doc*", MultiSelect = True, _
"Browse for files 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 = 1
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
End If
For iTable = 1 To TableNo
With .tables(iTable)
'copy cell contents from Word table cells to Excel cells in column A and B
Cells(ix + 1, "A") = WorksheetFunction.Clean(.Cell(1, 2))
Cells(ix + 1, "B") = WorksheetFunction.Clean(.Cell(2, 2))
Cells(ix + 1, "C") = WorksheetFunction.Clean(.Cell(3, 2))
Cells(ix + 1, "D") = WorksheetFunction.Clean(.Cell(4, 2))
Cells(ix + 1, "E") = WorksheetFunction.Clean(.Cell(5, 2))
Cells(ix + 1, "F") = WorksheetFunction.Clean(.Cell(6, 2))
Cells(ix + 1, "G") = WorksheetFunction.Clean(.Cell(6, 3))
Cells(ix + 1, "H") = WorksheetFunction.Clean(.Cell(7, 2))
Cells(ix + 1, "I") = WorksheetFunction.Clean(.Cell(8, 2))
Cells(ix + 1, "J") = WorksheetFunction.Clean(.Cell(9, 2))
Cells(ix + 1, "K") = WorksheetFunction.Clean(.Cell(10, 2))
Cells(ix + 1, "L") = WorksheetFunction.Clean(.Cell(13, 2))
End With
Next iTable
End With
Set wdDoc = Nothing
End Sub
我知道,我需要创建一个循环,但我不能改变任何的循环例子我类似的问题找到工作。
首先,非常感谢你对这个项目的帮助。我完全理解你对excel不是最好的解决方案意味着什么。这就是说,我得到一个关于以下内容的编译错误:Private Function GetCellValue(ByRef pTable As Word.Table,ByVal pRow As Long)As Variant说它没有被定义。我会尽力弄清楚,但如果你在线,首先,谢谢,其次,我只需要在顶部定义它? 编辑:具体的错误是,“用户定义的类型没有定义” –
任何机会,你没有赶上subtl的意见,我把代码放在设置引用到Word和Scripting库。它在主子程序中。您需要设置对Microsoft Word#。#Object Library和Microsoft Scripting Runtime库的引用。这应该照顾那个错误。 – dscarr
是的。 。 。该函数使用参数列表中的Word.Table对象。这需要对Word对象库的引用。 – dscarr