1

我有一些在 Word 2003 中开发的第 3 方代码,但它在 Word 2010 中不起作用。代码应粘贴 Excel.Chart 对象并将其转换为内联形状。

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
...

起初,该PasteAndFormat行引发了一条空的错误消息。

然后我试图用

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

它引发了另一个错误,说System Error &H80004005 (-2147467259). Unspecified error. 但在这种情况下,图表实际上会粘贴到 Word 中。

有人知道导致问题的原因以及应该如何解决?

TIA

4

1 回答 1

1

我找到了解决方案。首先,我用 PasteSpecial 替换了 PasteAndFormat。然后由于错误没有意义,我试图忽略它。它奏效了!这是代码:

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
...
于 2012-11-12T11:57:38.283 回答