我需要遍历Word文档中的所有对象以查找所有表并将它们复制粘贴到Excel工作表中。问题是某些表格在形状内。所以我最终为表格编写了一个循环,为形状表格编写了一个循环。所以现在我在Excel表格中将它们无序化了。但是对于我的最终解决方案,我需要将它们(形状中的表格和表格)按顺序排列,就像它们出现在Word文档中一样。
有没有人有一些建议。谷歌搜索一整天后,即使是小小的提示也是受欢迎的
到目前为止我有两个循环:
Dim Appwd As Word.Application
Set Appwd = CreateObject("Word.Application")
Appwd.Visible = True
Dim wdDoc As Word.Document
Set wdDoc = Appwd.Documents.Open("U:\path\to\test.docx")
Dim Tb As Table
Dim Shp As Object
Dim tab_anz As Integer
Dim lz As Integer
Dim i As Integer
lz = 1
For Each Tb In wdDoc.Tables
Tb.range.Copy
Sheets(1).Cells(lz + 2, 1).PasteSpecial Paste:=xlPasteValues
lz = Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell).Row
Next Tb
For Each Shp In wdDoc.Shapes
With Shp.TextFrame
If .HasText Then
Shp.TextFrame.TextRange.Copy
Sheets(1).Cells(lz + 2, 1).PasteSpecial Paste:=xlPasteValues
lz = Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell).Row
End If
End With
DoEvents
Next Shp
应该是什么样的:
For Each Object In WordDocument.Objects
If Object.type == Table Then
Copy into Excel sheet
Elseif Object.type == Shapes
If Table in Shape Then copy into Excel sheet
End If ' Only for tables or tables in shapes
Next Object
由于