Hello all...
I know, you'll curse my stupidity, but I know you lot are supreme when it comes to VBA challenges. I'm sure this one is easy for you, not for me unfortunately.
Running Office 2013 - I have a varying number of Word 2013 documents containing tables (i may need to choose the particular table number which should be the same across each document) in a specific folder. I need to extract the contents of each table into a single Excel Workbook/sheet. I know this is possible as I have found the code (there I go again!) to carry out that task for a single Word document.
My question therefore, based on the code below, is how do I adjust this such that each document in the specified folder is opened, and table data copied (etc)?
Does anyone feel up to helping? A beer has your name on it if you can successfully help me! (you only need to help with this, the rest of my problems, well, they're for me to deal with)
Here's the Code: (and thanks to www.exceltrainingvideos.com for this)
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:\users\jobloggs\desktop\appraisal\Worddocument.docx"
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 Integer
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 Word document", vbExclamation, "No Tables to Import"
Exit Sub
End If
'For i = 1 To Tble
i = 3
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
Bookmarks