1
votes

I used this code to copy a table from excel to word

    Sub exceltoword()
Dim rangeToCopy As Range
Set rangeToCopy = Range("A1").CurrentRegion
Dim wordApp As Word.Application
Set wordApp = New Word.Application
wordApp.Visible = True
Dim wordDoc As Word.Document
Set wordDoc = wordApp.Documents.Open("C:\Users\mohammad.taha\AppData\Roaming\Microsoft\Templates\ARABBANK -SALARIES STATEMENT.dotx")
wordDoc.Application.Selection.Find.Text = "H"
wordDoc.Application.Selection.Find.Execute
wordDoc.Application.Selection.MoveDown Unit:=wdLine
rangeToCopy.Copy
wordDoc.Words(1).PasteExcelTable False, False, False
End Sub

but the table gets pasted into the first line of the document, I want to paste the table to a specific location in the middle of the document, how should I modify this code?

1

1 Answers

0
votes

Try to use the code below:

Sub exceltoword()
    Dim rangeToCopy As Range
    Set rangeToCopy = Range("A1").CurrentRegion
    Dim wordApp As Word.Application
    Set wordApp = New Word.Application
    wordApp.Visible = True
    Dim wordDoc As Word.Document
    Set wordDoc = wordApp.Documents.Open("C:\Users\mohammad.taha\AppData\Roaming\Microsoft\Templates\ARABBANK -SALARIES STATEMENT.dotx")
    wordDoc.Application.Selection.Find.Execute "H"
    wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=1
    wordApp.Selection.TypeParagraph
    wordApp.Selection.TypeParagraph
    rangeToCopy.Copy
    wordApp.Selection.PasteExcelTable False, False, False
    End Sub

I've added two paragraphs. If in your template it does not look good, you can remove it.