PDA

View Full Version : word doc with multiple tables to master excel spreadsheet



josedotcom
10-06-2014, 10:19 AM
Hello,

I am stuck again!

I have a word template that when completed looks like the attached. I am looking to take the data from this (excluding the left hand column) and whack it on a master spreadsheet. In theory these forms will come in maybe 20 a week.

I have some code that does this, albeit in a messy way, so I am looking to utilise bookmarks and ensure the data is put into one row on the master spreadsheet.

The code I have:

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
tableNo = wdDoc.tables.Count
tableTot = wdDoc.tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
resultRow = 4
For tableStart = 1 To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = resultRow + 1
Next tableStart
End With
End Sub

snb
10-06-2014, 12:18 PM
Sub M_snb()
GetObject(, "Word.application").activedocument.tables(1).Range.Copy
Sheet1.Paste Sheet1.Cells(20, 1)
Sheet1.Cells(20, 1).CurrentRegion.Resize(30).UnMerge
Sheet1.Cells(20, 1).CurrentRegion.Resize(30).Columns(1).SpecialCells(4).EntireRow.Delete
End Sub

josedotcom
10-06-2014, 12:36 PM
Thank you, this code works, it pretty much does what the code I posted does, although a lot tidier! I am looking for this data to be in a single row on excel, losing the column with date and time, name etc headers, I also dont need the 'case details' and similar cells. This is why I thought bookmarks would work but, I've never worked with them to export to excel.

snb
10-06-2014, 03:29 PM
It's not clear to me which data you are interested in, but you could try


Sub M_snb()
GetObject(, "Word.application").activedocument.tables(1).Range.Copy
Sheet1.Paste Sheet1.Cells(20, 1)
with Sheet1.Cells(20, 1).CurrentRegion
.offset(.rows.count).Resize(30).clearcontents
end with
End Sub

josedotcom
10-07-2014, 05:39 AM
12368 12367

This is the master spreadsheet - I have set up the cells on the word doc with bookmarks enclosing each cell. I have also now uploaded the form with bookmarks. If these bookmarks are on a word template, will they transfer over to the completed form?

snb
10-08-2014, 03:27 AM
You will have to find out yourself which cells of the table to select.

This is the method:


Sub M_snb()
ReDim sn(0, 1 To 24)

With GetObject(, "Word.application").activedocument.tables(1)
For j = 1 To 24
sn(0, j) = Replace(Replace(.Cell(Choose(j, 1, 1, 1, 2, 2, 2, 3, 3, 3), Choose(j, 1, 2, 3, 1, 2, 3, 1, 2, 3)).Range.Text, Chr(7), ""), vbCr, "")
Next
End With

Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(, 24) = sn
End Sub