Save table in array, then paste from array into doc

D

dedawson

I have a table containing nested tables. I need to remove the nested
tables (replacing them with a placeholder), modify the parent table,
and then replace the nested tables.

In the first loop, I copy the table to the array, cut it from the doc,
and put in the placeholder.
In the second loop, I find the placeholder, and attempt to replace it
with the table from the array

I have no problem with any of this except getting the saved tables out
of the array and back into the doc. Here's hoping someone can turn on
a light bulb for me.

Dim TableRepository() As Variant
iEmbeddedTableCount = ActiveDocument.Tables(1).Tables().Count
ReDim TableRepository(iEmbeddedTableCount)
For i = iEmbeddedTableCount To 1 Step -1
ActiveDocument.Tables(1).Tables(i).Select
TableRepository(i) = Selection
Selection.Cut
Selection.TypeText Text:="TableRepository(" + str(i) + ")" + vbCrLf
Next i

Selection.Find.Wrap = wdFindContinue
For i = 1 To iEmbeddedTableCount
Selection.Find.Text = "TableRepository(" + str(i) + ")" + Chr(13)
Selection.Find.Execute
TableRepository(i).PasteAsNestedTable
' Selection.PasteAsNestedTable
Next i

End Sub ' SaveEmbeddedTables

thanks,
david
 
G

Gordon Bentley-Mix on news.microsoft.com

David,

Three points:

1. If I were doing this, I'd stay away from the Selection object as much as
possible (and the .Find method along with it) and re-organise the code to use
the Range object. It's cleaner, quicker and won't make the screen flicker.

2. I'd also look at using bookmarks as the placeholders. They're easier to
find in code and will work quite nicely if you use the Range object.

3. (This is the one that's probably most important to your problem.) From
the VBA help:

You can use PasteAsNestedTable only if the Clipboard contains a cell or
group of cells and the selected range is a cell or group of cells in the
current document.

I take this to mean that PasteAsNestedTable won't work to pull content
directly from an array. Instead, you would need to put the content into the
Clipboard and then paste it from there - and I have no idea how you would do
that.

Even so, I'm not sure that you can actually store a fully formatted table in
an array. I suspect that the formatting will be lost, and what's stored in
the array is just the content of the table. However, I may be wrong, and I'm
sure someone else will correct me.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
T

Tony Jollans

I think I would use AutoText as a way of saving the tables, and bookmarks as
a way of saving the place, something like ...

iEmbeddedTableCount = ActiveDocument.Tables(1).Tables().Count

to save, ..

For i = iEmbeddedTableCount To 1 Step -1
With ActiveDocument
Set rngEmbeddedTable = .Tables(1).Tables(i).Range
.AttachedTemplate.AutoTextEntries.Add "_TempName" & i,
rngEmbeddedTable
rngEmbeddedTable.Tables(1).Delete
.Bookmarks.Add "_TempName" & i, rngEmbeddedTable
Set rngEmbeddedTable = Nothing
End With
Next i

... and, to restore, ..

For i = iEmbeddedTableCount To 1 Step -1
With ActiveDocument
Set rngEmbeddedTable = .Bookmarks("_TempName" & i).Range
With .AttachedTemplate.AutoTextEntries("_TempName" & i)
.Insert rngEmbeddedTable, True
.Delete
End With
ActiveDocument.Bookmarks("_TempName" & i).Delete
Set rngEmbeddedTable = Nothing
End With
Next i
 
D

dedawson

Hi Gordon,

After a lot of playing around, I sort of ended up with the same
conclusion-- using an array wasn't going to work. Setting watches on
the array pretty well confirmed that the formatting would be lost
anyway. I finally solved the problem by writing the tables to a dummy
file.

I do appreciate the comment on using the Range object as a means of
avoiding screen flicker. That's an issue that has annoyed me for a
long time and I assumed I was stuck with it.

thanks much,
david (an old dog learning new tricks)
 
D

dedawson

Thanks Tony,

Owing to some sort of refresh issue, I didn't see your reply until
this morning, and I had already ended up doing essentially what you
proposed, but by writing the embedded tables to a dummy document
instead. I hadn't thought of using AutoText, which has the advantage
of eliminating the need to handle additional scratch files.

regards,

david
 

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