VBA新手,下面是我的单词vba代码,尝试在word文档中的所有表中读取单独的excel工作表(每个表一个)。我在第一行收到错误:
WS.Cells(i, j) = myTable.Cell(i, j)
说:
该集合的请求成员不存在
经过一些故障排除后问题似乎与myTable.Cell(i,j)有关,但是每个表的大小应该处理......?想法,建议?谢谢!
Sub ReadTablesToExcel()
Dim myTable As Table
Dim RowsCount As Integer
Dim ColumnsCount As Integer
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")
Dim oExcel1 As Object
Set oExcel1 = oExcel.Workbooks.Open("C:\Users\Mike\Desktop\Book3.xlsx")
For Each myTable In ActiveDocument.Tables
Dim WS As Object
Set WS = oExcel1.Activesheet
RowsCount = myTable.Rows.Count
ColumnsCount = myTable.Columns.Count
For i = 1 To RowsCount
For j = 1 To ColumnsCount
WS.Cells(i, j) = myTable.Cell(i, j)
Next j
Next i
Next myTable
ActiveDocument.Repaginate
End Sub
答案 0 :(得分:1)
您的代码可以在没有抛出错误的情况下运行。 ı只添加了为每个表添加新工作表的代码。
这适用:(Windows XP,Office 2007)
Sub ReadTablesToExcel()
Dim myTable As Table
Dim RowsCount As Integer
Dim ColumnsCount As Integer
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")
Dim oExcel1 As Object
Set oExcel1 = oExcel.Workbooks.Open("C:\Users\Mike\Desktop\Book3.xlsx")
For Each myTable In ActiveDocument.Tables
Dim WS As Object
oExcel1.Sheets.Add
Set WS = oExcel1.ActiveSheet
RowsCount = myTable.Rows.Count
ColumnsCount = myTable.Columns.Count
For i = 1 To RowsCount
For j = 1 To ColumnsCount
WS.Cells(i, j) = myTable.Cell(i, j)
Next j
Next i
Next myTable
oExcel1.Close (True) 'Closes the workbook by saving changes.
Set oExcel1 = Nothing
ActiveDocument.Repaginate
End Sub