PDA

View Full Version : [SOLVED:] Insert tables from Collection



gmaxey
08-02-2015, 06:09 AM
Hi,

I have a large document with one main table with hundreds of cells. Each of these cells contains a single nested (child) table with variable data. Now need to sort these child tables tables in the main table based on a data field I the child table.

My thought was I could run a routine that adds the tables to a collection in the correct order the dump the collection of tables into a new empty main table in the correct order.

I can't figure out how to insert a table from the collection or set an existing table = to a collection table. The best I an do is insert content of a collection table.

Any ideas?

Here is a rough example:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCol As New Collection
Dim oTbl As Table
Dim oDoc As Document
Dim lngIndex As Long
For Each oTbl In ActiveDocument.Tables
oCol.Add oTbl
Next oTbl
'I now have a collection of table objects.
Set oDoc = Documents.Add
'I now want to insert the collection of tables in this new document
For lngIndex = 1 To oCol.Count
oDoc.Range.InsertAfter oCol.Item(lngIndex) & vbCr
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

Jay Freedman
08-04-2015, 07:40 PM
The code needs a couple of adjustments.

First, the expression For Each oTbl In ActiveDocument.Tables only captures the outer table, and the collection has only one member. Change that to For Each oTbl In ActiveDocument.Tables(1).Tables to grab only the nested tables.

The other problem is that the Range.InsertAfter method's parameter is explicitly of data type String. VBA is coercing the table to a string. That's why you're getting the content of the table instead of the table itself. There isn't any method like InsertTable, so the next best thing is to use a Range object's .FormattedText property -- which is misleading because it isn't explicitly a String, and could be a whole table.

The last hurdle is that the items in the collection are of type Variant/Table, but they have no properties. Specifically, they don't have a Range property, which you need in order to get the FormattedText property. To coerce each item back to the Table data type, you have to assign the item to a Table variable.

Wrapping that up, here's a version that works, leaving you the job of sorting the collection in whatever way you want.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCol As New Collection
Dim oTbl As Table
Dim oDoc As Document
Dim lngIndex As Long
Dim rg As Range

For Each oTbl In ActiveDocument.Tables(1).Tables
oCol.Add oTbl
Next oTbl
'I now have a collection of table objects.
Set oDoc = Documents.Add

'I now want to insert the collection of tables in this new document
For lngIndex = 1 To oCol.Count
Set oTbl = oCol.Item(lngIndex) ' coerce to Table type
Set rg = oDoc.Range
rg.Collapse wdCollapseEnd ' prepare location for insert
rg.FormattedText = oTbl.Range.FormattedText ' do the insert
rg.Collapse wdCollapseEnd
rg.InsertAfter vbCr ' add the separating paragraph
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

gmaxey
08-04-2015, 08:54 PM
Jay,

Thanks. Yes my example code was overly simplistic. in practice I am trying sort nested tables contained in a parent table an rearrange them in the parent table. I can work out the sort and put the tables in a collection sorted properly. What I can't seem to do is recreate (place) the sorted nested tables in the correct cell of the parent table. They are in inserted a) between existing rows or (b) in the next cell over from where the code tries place them.

Hopefully the attached with illustrate better.

This post is related to my other post here on cut/paste nested tables

http://www.vbaexpress.com/forum/showthread.php?53355-Cut-and-Paste-Nested-Tables

gmaxey
08-06-2015, 10:08 AM
Ok, with some help from Jay and then tripping over what should have been obvious, I've word out a solution.

The attached document contains commented code to sort and reposition nested tables based on a value defined in the nested tables. Empty cell between the first ad last nested table are closed up.


Some notes to other unwary souls who might wonder into this rabbit hole.

1. If you add a document table to a collection, then delete the table from the document a strange thing happens. The collection count remains the same and the collection item is still there but it is not a table anymore. All the attributes of the collection item will return "object not defined.

2. Nesting tables in a parent table cells using .formattedText can create a royal mess. Define the table in temporary document then copy and PasteAsNestedTable.


Thank Jay Freeman for your nudge here and to others who might have looked.