PDA

View Full Version : Copy/paste table as separate objects



stecstec
01-03-2007, 04:10 AM
Hi there - could anyone assist me with the following routine?

I'm trying to copy and paste a table, but my problem is that I need it as a separate table from the one it is copying.

- So far I can only get it paste as new cells on the original table.

Heres the code (I'm using bookmark refs to identify the table I need to copy):

Sub CopyTable(Bookmark As String)
On Error GoTo errorhandler

Dim sNumber As Integer

ActiveDocument.Bookmarks(Bookmark).Select
Selection.Collapse wdCollapseStart
sNumber = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
ActiveDocument.Tables(sNumber).Range.Copy

Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertParagraphAfter
Selection.Paste

End Sub


Any assistance would be appreciated!

Steve.

fumei
01-04-2007, 07:05 AM
Your bookmark is IN the table, yes? So...

1. You Select the bookmark.
2. Collapse it. (Selection is IN the table)
3. You get the count, and copy the Range. Selection is still IN the table.
4. You insert a paragraph - Selection is still IN the table.
5. You paste the Copy...the Selection is still IN the table.

So, yes, the copy will be pasted in the table.

Question. Is the bookmark a single character (that is, it is a location), or is the bookmark containing the whole table?

If you have bookmarked the table itself (select the table and use Insert > Bookmark), then to make a copy of it becomes very easy.

To make a copy of the bookmarked table at the current Selection point:Sub CopyTable(Bookmark As String)

Dim r As Range
Set r = ActiveDocument.Bookmarks(Bookmark).Range
r.Copy

Selection.InsertParagraphAfter
Selection.Paste
Set r = Nothing
End SubYou have no need to either go to the bookmark, or select it. You simply copy the Range of the bookmark (in this case, the whole table), and paste it.

Obviously you could use this to paste it anywhere you like - another bookmark...whatever.

You can use this with any bookmark range. So you could have a bookmark of only part of a table. Note that you can nest bookmarks. That is, you could bookmark the whole table, AND make a separate bookmark of some of the table...or anything else for that matter.

Generally, it is better to use Ranges, rather than selecting things.

fumei
01-04-2007, 07:13 AM
Also, by bookmarking tables themselves, you can avoid dealing with figuring out table numbers, as in:

ActiveDocument.Tables(sNumber)

BTW: if you DID just have the bookmark as a location (not the whole table), you can still do this easily. As long as the bookmark is IN the table (and it does not matter where), you can use .Expand.Dim r As Range
Set r = ActiveDocument.Bookmarks(Bookmark).Range
r.Expand Unit:=wdTable
r.Copy

Selection.InsertParagraphAfter
Selection.Paste
Set r = NothingThis expands the range to include the entire table the Range object is in.

Correctly speaking, you should test first to make sure the bookmark actually IS in a table. But I am assuming that it is.

fumei
01-04-2007, 07:55 AM
More: if you are working with tables a lot, it is a good idea to put a bookmark in them all. That way you can access them much much easier, without hunting for them, without selecting them etc. etc.

stecstec
01-04-2007, 09:11 AM
Many thanks for your advice Fumei,

To answer your earlier question, I'm using a bookmark within one of the cells in the table.

Just one further query, what would be the best way to paste the table at another bookmark reference?

Cheers, Steve.

fumei
01-04-2007, 11:04 AM
The best way? Paste it at the bookmark. Other than that I really can't say because YOU don't say. Before it? After it? Does it have a chunk of text as bookmark already? is it just a single location range?

The best way? You tell me. What have you done so far? What have you tried? And what are you trying to do?

stecstec
01-05-2007, 01:15 AM
Basically I just need to copy the table and paste it underneath the first one (with a bit of whitespace between them). The bookmark I'm using is just to reference the section in the document and shouldn't contain any text.

So is it possible to copy a table from one bookmark, then paste into another bookmark?

Thanks again!

lucas
01-05-2007, 07:02 AM
Basically I just need to copy the table and paste it underneath the first one (with a bit of whitespace between them).
attachment using Gerry's code and advice.