【发布时间】:2015-07-15 15:20:32
【问题描述】:
我是 Excel 宏和 VBA 的新手。
我需要使用宏 VBA 将表格数据从 Word 文档复制到 Excel 工作表。
我需要在特定文件夹中的多个文档版本中执行文档 V1.2 版本。
例如:我有文件 "C:\Test\FirstDocV1.1.doc" 和 "C:\Test\FirstDocV1.2.doc"。
我只想执行"C:\Test\FirstDocV1.2.doc" 并获取表数据。
无论如何我都试过了,但它说的是“没有桌子”。
查看我的代码如下。
Sub importTableDataWord()
Dim WdApp As Object, wddoc As Object
Dim strDocName As String
On Error Resume Next
Set WdApp = GetObject(, "Word Application")
If Err.Number = 429 Then
Err.Clear
Set WdApp = CreateObject("Word Application")
End If
WdApp.Visible = True
strDocName = "C:\Test\FirstDocV1.2.doc"
'I am manually giving for version 1.2 doc. But I need to select which contains v1.2 version automatically from Test folder.
If Dir(strDocName) = "" Then
MsgBox "The file is not present" & strDocName & vbCrLf & " or was not found"
Exit Sub
End If
WdApp.Activate
Set wddoc = WdApp.Documents(strDocName)
If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
wddoc.Activate
Dim Tble As Integer
Dim rowWd As Long
Dim colWd As Long
Dim x As Long, y As Long
x = 1
y = 1
With wddoc
Tble = wddoc.tables.Count
If Tble = 0 Then
MsgBox "No Tables Found in the document"
Exit Sub
End If
For i = 1 To Tble
With .tables(i)
For rowWd = 1 To .Rows.Count
For colWd = 1 To .Columns.Count
Cells(x, y) = WorksheetFunction.Clean(.cell(rowWd, colWd).Range.Text)
y = y + 1
Next colWd
y = 1
x = x + 1
Next rowWd
End With
Next
End With
wddoc.Close savechanges:=False
WdApp.Quit
Set wddoc = Nothing
Set WdApp = Nothing
End Sub
谁能帮帮我。
【问题讨论】: