teepee
teepee

Reputation: 2714

Copying chart from Excel to Word

I am trying to copy a chart into Word. I looked up a method that says to do something like this:

Dim wordApp As Object
Dim wordDoc As Object
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True

Set wordDoc = wordApp.Documents.Add

With ThisWorkbook
    .Sheets("Sheet1").ChartObjects(1).Activate
    .ActiveChart.ChartArea.Copy
End With
With Selection
    .PasteSpecial Link:=False, DataType:=wdInLine, _
    Placement:=wdInLine
End With
wordApp.Documents.Close
wordApp.Application.Quit

I tried to change the With selection section to just wordApp.Documents.Selection.Pastespecial which causes some weird things to happen and ends up somehow pasting a screen shot of the first sheet in the workbook, and crashing Excel. Any suggestions appreciated.

Upvotes: 0

Views: 550

Answers (1)

Felipe Rosa
Felipe Rosa

Reputation: 376

You have a problem in this part of your code:

With Selection
    .PasteSpecial Link:=False, DataType:=wdInLine, _
    Placement:=wdInLine
End With

you can solve your problem changing it with this:

With wordDoc.Application.Selection
    .PasteSpecial Link:=False, DataType:=wdInLine, _
    Placement:=wdInLine
End With

Explanation: Running your code from an excel macro, the Selection object will resolve to the current selection in your excel sheet, you need to specify the scope of the selection to apply your command on your word document instead of your current Excel workbook.

Upvotes: 1

Related Questions