我是Excel Macro和VBA的新手。
我需要使用宏VBA将表格数据从word文档复制到Excel工作表。
我需要在特定文件夹中的许多版本的文档中执行文档V1.2版本。
例如:我有文件"C:\Test\FirstDocV1.1.doc"
& "C:\Test\FirstDocV1.2.doc"
。
我只想执行"C:\Test\FirstDocV1.2.doc"
并获取表数据。
我无论如何都试过了,但它说的是#34;没有表格#34;。
请参阅下面的代码。
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
任何人都可以帮助我。