I am newbie to Excel Macro and VBA.
I have a requirement to copy the table data from a word document to an excel sheet using macro VBA.
I need document V1.2 version to be executed among many version of documents in a specific folder.
For ex: I have documents "C:\Test\FirstDocV1.1.doc"
& "C:\Test\FirstDocV1.2.doc"
.
I want only "C:\Test\FirstDocV1.2.doc"
to be executed and fetch the table data.
I tried anyhow, but it is saying as "No tables".
See my code as below.
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
Can anyone help me please.