How to Convert Array to Table

K

kdw

I know how to convert a recordset to a table using

varData=rst
With rng
..InsertAfter varData
Set objWordTable = .ConvertToTable
End with

But this doesn't work for an array. Can anyone suggest a solution? I am
doing this via automation in Access.
Thank you!
 
D

Doug Robbins - Word MVP

Maybe this will help

Procedure to sort a two dimension array:

Dim MyArray() As Variant, i As Integer, j As Integer, m As Integer, n As
Integer, target As Document, newtable As Table, myitem As Range

'Load client data into MyArray

MyArray = ListBox1.List()

' Create a new document containing a table

Application.ScreenUpdating = False

Set target = Documents.Add

Set newtable = target.Tables.Add(Range:=target.Range(0, 0),
numrows:=ListBox1.ListCount, NumColumns:=2)

' Populate the cells of the table with the contents of the array

For i = 1 To ListBox1.ListCount

For j = 1 To 2

newtable.Cell(i, j).Range.InsertBefore MyArray(i - 1, j - 1)

Next j

Next i

' sort the table

newtable.Sort ExcludeHeader:=False, FieldNumber:="Column 2",
SortFieldType:=wdSortFieldDate, SortOrder:=wdSortOrderDescending

i = newtable.Rows.Count

' Get the number of columns in the table of client details

j = 2

' Set the number of columns in the Listbox to match

' the number of columns in the table of client details

ListBox1.ColumnCount = 2

' Define an array to be loaded with the client data

Dim NewArray() As Variant

'Load client data into MyArray

ReDim NewArray(i, j)

For n = 0 To j - 1

For m = 0 To i - 1

Set myitem = newtable.Cell(m + 1, n + 1).Range

myitem.End = myitem.End - 1

NewArray(m, n) = myitem.Text

Next m

Next n

' Load data into ListBox1

ListBox1.List() = NewArray

target.Close wdDoNotSaveChanges

Application.ScreenUpdating = True


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top