Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2019, 05:14 PM
fly545 fly545 is offline Want to copy & paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy & paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy & paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 10-02-2019, 07:01 PM
Guessed's Avatar
Guessed Guessed is offline Want to copy & paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy & paste a table underneath itself x no. of time, table found through style heading name Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 10-02-2019, 08:55 PM
fly545 fly545 is offline Want to copy & paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy & paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy & paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

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.
Attached Files
File Type: docx All Data Sources.docx (14.9 KB, 8 views)
Reply With Quote
  #4  
Old 10-03-2019, 05:15 AM
Guessed's Avatar
Guessed Guessed is offline Want to copy & paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy & paste a table underneath itself x no. of time, table found through style heading name Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 10-03-2019, 03:52 PM
fly545 fly545 is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 10-05-2019, 04:44 AM
Guessed's Avatar
Guessed Guessed is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 10-06-2019, 11:54 PM
fly545 fly545 is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

can i name all my tables? there are multiple tables in this word document.
Reply With Quote
  #8  
Old 10-07-2019, 03:17 AM
Guessed's Avatar
Guessed Guessed is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #9  
Old 10-07-2019, 02:56 PM
fly545 fly545 is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

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).
Reply With Quote
  #10  
Old 10-07-2019, 08:17 PM
fly545 fly545 is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

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
is there a way to do that?
Reply With Quote
  #11  
Old 10-12-2019, 12:20 AM
Guessed's Avatar
Guessed Guessed is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #12  
Old 01-16-2020, 05:53 PM
fly545 fly545 is offline Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Windows 10 Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Office 2016
Novice
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name
 
Join Date: Oct 2019
Posts: 7
fly545 is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
copy and paste, headings, table

Thread Tools
Display Modes


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
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name 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
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name Heading 1 and Table & Figures style mismatch ezrayst93 Word 5 07-21-2014 11:06 PM
Want to copy &amp; paste a table underneath itself x no. of time, table found through style heading name 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:44 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft