2
votes

I'm trying to copy text and charts from Excel to Word. The problem is that the chart is always appearing on top of the Word document. How can I add the chart at the end of the Word document? Here is my code:

Sub Test()

Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table


Set WordApp = GetObject(class:="Word.Application")
WordApp.Visible = True
WordApp.Activate

'Create a New Document
Set myDoc = WordApp.Documents.Add

'Copy Excel Text in cell A1 to A3
Worksheets("Rapportage").Select
Range("A1:A3").Select
Selection.Copy

'Paste Excel Text into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
     
'Copy Excel Chart
Worksheets("Rapportage").Select
Range("A4").Select
Selection.Copy

'Paste Chart into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

End Sub
2
Instead of myDoc.Paragraphs(1).Range.PasteExcelTable I imagine the syntax should be something like myDoc.Paragraphs(myDoc.Paragraphs.Count+1).Range.PasteExcelTableMarcucciboy2

2 Answers

1
votes

Replace

'Paste Excel Text into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

with

'Paste Excel Text into MS Word
'add a paragraph at the end of the document and paste into it
with myDoc.Content
    .InsertParagraphAfter
    .Paragraphs.Last.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
End With
0
votes

This happens because in both paste commands you are pasting in Paragraphs(1). Instead, you could just paste using Selection, so it will paste one thing after another in the order that you want (it pastes where the cursor is):

'Paste Excel Text into MS Word
     WordApp.Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

And, in the end:

'Paste Chart into MS Word
     WordApp.Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False