将图像复制粘贴到图表不正确地粘贴空白图像

时间:2014-10-22 16:41:53

标签: excel vba charts

我对此问题提出了一个切线问题:Using VBA Code how to export excel worksheets as image in Excel 2003?

具体来说,当宏将范围的副本粘贴到图表时,即使复制的范围包含5个图表和一些格式化的单元格,图像也是空白的。当我手动执行完全相同的步骤时,它们都按预期工作。

即使更奇怪,我已经记录了除导出步骤之外的整个过程。当我运行录制的宏时,它可以工作。但是当我从下面的For Each循环中的录制宏中复制代码,并将其调整为指向宏所处理的工作表(即用“t”替换“ActiveSheet”)时,宏不再起作用。

我甚至在使用For Each移动到每张纸之后只调用录制的宏,仍然粘贴了空白图像。

我很感激你的帮助。

我的代码:

Sub ExportCharts()
Dim Rng As Range
Dim S As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Dim EName As String
Dim CO As ChartObject
Dim C As Chart
Dim temp As String

Application.ScreenUpdating = False

'Iterate through the sheets in the workbook
For Each t In wb.Worksheets
    'Capture the sheet
    Set S = t

    S.Activate
    'Set the range to be exported
    Set Rng = S.Range("A1:Z60")

    'Copy range as picture onto Clipboard
    Rng.Select
    Rng.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

    'Build the chart/file name
    EName = S.Name & " Quality Charts"

    'Create an empty chart with exact size of range to be copied
    S.Range("$AA$1:$AC$2").Select
    ActiveSheet.Shapes.AddChart.Select
    Set C = ActiveChart
    temp = Right(C.Name, Len(C.Name) - 1 - Len(S.Name))

    S.Shapes(temp).Height = Rng.Height
    S.Shapes(temp).Width = Rng.Width

    'Paste into chart area, export to file, delete chart
    'C.Activate
    With C
        .Paste
        .Export "\\COMPUTERNAME\Users\USERNAME\Desktop\My Documents\" & EName & ".jpg"
        'Note the above is an actual hard coded path in my code (yes I want it hard coded)
    End With
    C.Delete
Next

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

所以我终于弄明白了这个问题。关闭屏幕更新的线是问题。

人们会认为这是因为我说屏幕上显示的副本(为什么MS不允许该命令使用显示,因为它在最后一次屏幕更新时显示超出我的范围,但它不完全像Excel是没有错误的。)

无论如何,注释掉该行会产生良好的粘贴效果。

作为那些为了获得合理的运行速度而在那里有更多宏观和想要/需要屏幕更新的人的注释,在我发现问题之后我尝试在复制之前重新激活屏幕更新作为图片,然后之后立即再次关闭它,这很有效。

相关问题