|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Want to copy & paste a table underneath itself x no. of time, table found through style heading name
I have a large word document with numerous number of headings and tables.
Each table is specific to a heading, either level 1 or level 2 style heading. I want to find a table based on the style heading title and duplicate that table x no. of times depending on the input. Want to be able to paste the duplicate tables under the existing table with a return in between. |
#2
|
||||
|
||||
Do you have a sample doc which would be the starting point?
Are you asking for the number of duplicates each time it finds a table or just specifying the number of repeats once?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
only specifying the number of repeats once at the beginning of the document (note, the document will be a template in the end). I've attached an example.
|
#4
|
||||
|
||||
If your tables have at least one empty paragraph after each one (and two at the end of the doc) then the following code will work
Code:
Sub Temp3() Dim i As Integer, iCount As Integer, aRng As Range, aTblRng As Range, j As Integer iCount = InputBox("How many duplicates?", , "1") For i = ActiveDocument.Tables.Count To 1 Step -1 Set aRng = ActiveDocument.Tables(i).Range.Paragraphs.First.Previous.Range aRng.MoveEnd Unit:=wdCharacter, Count:=-1 If aRng.ParagraphFormat.OutlineLevel < wdOutlineLevel3 Then Set aTblRng = ActiveDocument.Tables(i).Range aTblRng.End = aTblRng.Paragraphs.Last.Next.Range.End Set aRng = aTblRng.Duplicate For j = iCount To 1 Step -1 aRng.Collapse wdCollapseEnd aRng.FormattedText = aTblRng.FormattedText Next j End If Next i End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
hi Guessed,
that seems like it would work to copy my tables (no problem with adding in those additional paragraphs) but it doesn't cover the "look" function that i would like to use, where i find the heading title and then copy the table beneath. here is an example of the code i was trying to make work (assuming those paragraphs weren't there), note, it doesn't work and the code gets stuck on the first selection criteria. With Selection.Find .Text = "Data A" .Style = "Heading 2a" .Execute If .Found Then End If End With With Selection .MoveDown Unit:=wdLine, Count:=1 .Tables(1).Range.Select .Copy .MoveDown Unit:=wdLine, Count:=1 .MoveDown Unit:=wdLine, Count:=1 .TypeParagraph .MoveUp Unit:=wdLine, Count:=1 .Style = ActiveDocument.Styles("SEW body text") .TypeParagraph .PasteAndFormat (wdFormatOriginalFormatting) End With |
#6
|
||||
|
||||
It looks like you actually want something different to what you asked for in the first post.
If you want to find a specific table then you should bookmark it and then have the code duplicate that range. Looking for a specific table based on the heading above it is a less direct method.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
can i name all my tables? there are multiple tables in this word document.
|
#8
|
||||
|
||||
You can't name the table itself but you can use other methods to identify particular tables.
For instance you can place a bookmark in a table and then use something along the lines of ActiveDocument.Bookmarks("bmname").Range.Tables(1) The beauty of using a bookmark is that it can't exist in more than one place so if you copy and paste that table, the copies won't also have the bookmark in them. You can also iterate through the tables to find each one that matches your pattern. eg For each aTable in ActiveDocument.Tables if aTable.Style = "Fred" then 'do something end if Next aTable
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
OK, So what you're saying is that i bookmark each table in the document with a different name, then i can call up the active document's bookmarks and select the range based on those bookmarks?
Then using your code: Sub Temp3() Dim i As Integer, iCount As Integer, aRng As Range, aTblRng As Range, j As Integer iCount = InputBox("How many duplicates?", , "1") For i = ActiveDocument.Tables.Count To 1 Step -1 Set aRng = ActiveDocument.Tables(i).Range.Paragraphs.First.Pr evious.Range aRng.MoveEnd Unit:=wdCharacter, Count:=-1 If aRng.ParagraphFormat.OutlineLevel < wdOutlineLevel3 Then Set aTblRng = ActiveDocument.Tables(i).Range aTblRng.End = aTblRng.Paragraphs.Last.Next.Range.End Set aRng = aTblRng.Duplicate For j = iCount To 1 Step -1 aRng.Collapse wdCollapseEnd aRng.FormattedText = aTblRng.FormattedText Next j End If Next i End Sub I can then duplicate the bookmarked table into the paragraph after the table? If i wanted to have multiple duplications, how would you suggest i go about that? (say i wanted 4 copies of the table instead of 1 based on an input). |
#10
|
|||
|
|||
So I have almost gotten through to the end, however, whenever icount >2 the table pastes next to the previous table it pasted when i need it within its own table below.
Code:
Dim i As Integer Dim iCount As Integer Dim Rng As Range iCount = InputBox("How many duplicates?", , "1") Set Rng = ActiveDocument.Bookmarks("Generator").Range If iCount = 0 Then Rng.Select Rng.Delete Else For i = iCount To 2 Step -1 Set Rng = ActiveDocument.Bookmarks("Generator").Range Rng.Select Rng.Collapse wdCollapseEnd Rng.Select Rng.Paste 'insert paragraph here Next i End If |
#11
|
||||
|
||||
Can you explain the endgame for these macros? It appears you are trying to expand a form for repetitive sections. In principle this would not be a copy/paste activity as the source tables may already have content that was specific to that instance.
If you want to expand the number of tables using fresh empty forms rather than duplicating tables that may have been sullied, it would be better to store copies of each table as a building block and placing copies of those at your desired bookmark location.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#12
|
|||
|
|||
I fixed the issue by adding into the assigned bookmarks some enters on either side of the table, this way they paste below and not inside the table.
I couldn't store the information in the macro due to accessibility for other people to edit the bookmarks. the above is a bit sneaky but it works well. |
Tags |
copy and paste, headings, table |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Looking to copy select cells in table using dropdown list to paste to new table in another worksheet | CaptainRetired | Excel Programming | 18 | 01-04-2018 07:22 PM |
Is it possible to copy non-contiguous rows of a Table and paste them as a separate Table in Word? | Joey Cheung | Word Tables | 1 | 08-12-2014 05:15 PM |
Heading 1 and Table & Figures style mismatch | ezrayst93 | Word | 5 | 07-21-2014 11:06 PM |
Layered or Nested Table difficulty--can't edit the table underneath | theheartsmaster | Word Tables | 1 | 12-10-2012 04:10 AM |
How to have this Heading – Table of contents style? | Jamal NUMAN | Word | 0 | 01-13-2011 06:02 PM |