Copy and Paste a table within a Word from Excel

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
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!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try:
Code:
Sub Demo()
Dim Rng1 As Range, Rng2 As Range
With ActiveDocument
  Set Rng1 = .Tables(2).Range
  With Rng1
    .Start = .Start - 1
    Set Rng2 = .Duplicate
    With Rng2
      .Collapse wdCollapseStart
      .FormattedText = Rng1.FormattedText
    End With
  End With
End With
End Sub
 
Last edited:
Upvote 0
Hi, thanks for the reply - I've tried it but I'm getting a run time error 13, Type Mismatch on the following line;

Code:
 Set Rng1 = .Tables(2).Range

Thanks!
 
Upvote 0
The code was written as a stand-alone Word macro. To use it in an automation context, try changing:
Dim Rng1 As Range, Rng2 As Range
to:
Dim Rng1 As Word.Range, Rng2 As Word.Range
or, if you're using late binding:
Dim Rng1 As Object, Rng2 As Object
and changing:
ActiveDocument
to:
WordDoc
 
Last edited:
Upvote 0
Paul - can I request a further tweak if at all possible?

I need to repeat this routine up to 20 times, depending on the value of a cell. So is there an efficient method I could use to repeat it?
 
Upvote 0
Perhaps you could explain your requirements in more detail. What do you want - up to 20 copies of the same table or copies of up to 20 tables?
 
Upvote 0
Sorry, that would help - 20 copies of the same table please.
 
Upvote 0
Try something along the lines of:
Code:
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(2).Range
    With Rng1
      .Start = .Start - 1
      Set Rng2 = .Duplicate
      With Rng2
        .Collapse wdCollapseStart
        .FormattedText = Rng1.FormattedText
      End With
    End With
  Next
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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