MRB
MRB

Reputation: 452

Word 2010 VBA Paste Excel Chart Object

I have some 3rd party code that was developed in Word 2003, but it's not working in Word 2010. Code should paste an Excel.Chart object and convert it into an inline shape.

Sub PasteChartAsInteractive(chart As Excel.chart)
Dim myShape As Shape

chart.ChartArea.Copy
Selection.Style = ActiveDocument.Styles("Normal")
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.PasteAndFormat (wdChart)

Set myShape = Selection.Paragraphs(1).range.InlineShapes(1).ConvertToShape
myShape.ConvertToInlineShape
...

At first, the PasteAndFormat line was raising an empty error message.

I then tried to replace that line with

Selection.PasteSpecial Link:=False, DataType:=wdPasteOLEObject, Placement _
    :=wdInLine, DisplayAsIcon:=False

And it raised another error, saying System Error &H80004005 (-2147467259). Unspecified error. But in this case the chart actually pastes into Word.

Does someone know what is causing the problem and how it should be solved?

TIA

Upvotes: 1

Views: 3459

Answers (1)

MRB
MRB

Reputation: 452

I found a solution. First I replaced PasteAndFormat with PasteSpecial. Then since error makes no sense I tried to ignore it. And it worked! Here is the code:

Sub PasteChartAsInteractive(chart As Excel.chart)
Dim myShape As Shape

chart.ChartArea.Copy
Selection.Style = ActiveDocument.Styles("Normal")
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
' Selection.PasteAndFormat (wdChart)
On Error Resume Next
Selection.PasteSpecial Link:=False, DataType:=wdPasteOLEObject, Placement _
    :=wdInLine, DisplayAsIcon:=False
Set myShape = Selection.Paragraphs(1).range.InlineShapes(1).ConvertToShape
myShape.ConvertToInlineShape
...

Upvotes: 1

Related Questions