Excel VBA - 无法获取 Pictures 类的 Paste 属性

问题描述 投票:0回答:1

我有以下 VBA 代码片段。宏出错,并在下面指示的行上显示“无法获取图片类的粘贴属性”消息。

我正在尝试复制“源”工作表上的所有图表,并将它们作为图片粘贴到“目标”工作表中。它正在复制相当多的图表,但随后出现错误。

For Each chartObj In wsSrc.ChartObjects
    Application.CutCopyMode = False
    Sheets("Source").Select
    ActiveSheet.ChartObjects(chartObj.Name).Activate
    DoEvents
    ActiveChart.ChartArea.Copy
    DoEvents
    Sheets("Destination").Select
    Range("G" & topShift).Select
    DoEvents
    ActiveSheet.Pictures.Paste.Select ' LINE THAT IS ERRORING OUT
    DoEvents

    topShift = topShift + 29
Next chartObj

我尝试了一些在网上找到的其他方法,包括在获取范围时尝试直接粘贴,但我仍然收到错误消息。

excel vba
1个回答
0
投票
Sub test()


Dim wsSrc As Worksheet
Dim wsDest As Worksheet
Dim chartObj As ChartObject
Dim topShift As Long

    Set wsSrc = Sheets("Source")
    Set wsDest = Sheets("Destination")
    topShift = 1  ' Starting row for paste position
    
    For Each chartObj In wsSrc.ChartObjects
        ' Copy chart as picture
        chartObj.Chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    
        ' Paste in Destination sheet at specified range
        wsDest.Paste Destination:=wsDest.Range("G" & topShift)
        
        ' Increment top position for the next chart
        topShift = topShift + 29
    Next chartObj

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.