View Single Post
 
Old 07-30-2019, 12:39 AM
Dzib Dzib is offline Windows 10 Office 2019
Novice
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default Copy / Paste from Excel to Word template

Hi,
I have this macro that copy paste a table from Excel to Word but I'd like to paste it into an existing Word document and at the end of it, after a title and another table.
The table is always inserted before the title...

Code:
Sub Export()

 

Dim wdApp As Object

Dim wd As Object

Dim xlSheet As Worksheet

Dim rng As Range

Dim LastRow As Long

Const wdReplaceAll As Long = 2

Const wdFindContinue As Long = 1

 

On Error Resume Next

Set wdApp = GetObject(, "Word.Application")

 

If Err.Number <> 0 Then

    Set wdApp = CreateObject("Word.Application")

End If

On Error GoTo 0

 

Set wd = wdApp.Documents.Add("C:\Users\446066523\Documents\Test.docx")

wdApp.Visible = True

 

Set xlSheet = ActiveWorkbook.Sheets("INV")

With xlSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    Set rng = .Range("A2:C" & LastRow)

    rng.Copy

    With wd.Range

        .Collapse Direction:=0

        .InsertParagraphAfter

        .PasteSpecial DataType:=1

        With .Find

            .ClearFormatting

            .Text = vbTab

            .Replacement.ClearFormatting

            .Replacement.Text = " "

            .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue

        End With

    End With

End With
End Sub
Reply With Quote