Copy and Paste a table within a Word from Excel

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Guys,

I'm am playing with a routine that opens a Word document and then copies an existing Table within that document before pasting it underneath. The problem that I'm having is that the pasting is in effect attaching it to the previous Table with no break, whereas I need a new line started so the new Table is completely separate.

Here's what I have if someone can help me?

Code:
With WordDoc

.Tables(2).Range.Copy
.Tables(2).Range.Paste

End With

I was unsure if this should be posted in this forum, because it's playing with Word, or if it should go in another because it's being done from Excel - if it's in the wrong one then sorry!
 
Paul.....I've tried this on a new project but it is still pasting the new table directly underneath the previous one, in effect creating 1 long table.

Does the way in which the document is formatted have any effect? I've tried adding a new line directly under the 1st table and saving the document before running the code but it makes no difference.

Code:
Set WordDoc = WordApp.ActiveDocumentDim Rng1 As Object, Rng2 As Object, i As Long, j As Long
'Dim Rng1 As Word.Range, Rng2 As Word.Range, i As Long, j As Long
i = CLng(InputBox("How many copies?", "Table Replicator", 1))
With WordDoc
  For j = 1 To i
    Set Rng1 = .Tables(1).Range
    With Rng1
      .Start = .Start - 1
      Set Rng2 = .Duplicate
      With Rng2
        .Collapse wdCollapseStart
        .FormattedText = Rng1.FormattedText
      End With
    End With
  Next
End With
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
In that case, you could use:
Code:
Set WordDoc = WordApp.ActiveDocument
Dim i As Long, j As Long
i = CLng(InputBox("How many copies?", "Table Replicator", 1))
With WordDoc.Range.Tables(1).Range
  For j = 1 To i
    .Characters.Last.Next.InsertBefore vbCr & vbCr
    .Characters.Last.Next.Next.FormattedText = .FormattedText
  Next
End With
 
Upvote 0

Forum statistics

Threads
1,223,681
Messages
6,173,814
Members
452,535
Latest member
berdex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top