尝试通过Excel VBA宏保存GIF屏幕截图时出错

时间:2013-01-02 06:33:32

标签: excel vba excel-vba excel-2007

我使用下面的链接获取excel的屏幕截图并保存为.gif文件:

http://dmcritchie.mvps.org/excel/xl2gif.htm

当我尝试运行宏时,它在“containerbok.Activate”处出现以下错误:

运行时错误'424':对象必需

我可以知道为什么会收到此错误吗?

我正在使用Excel 2010

谢谢!

1 个答案:

答案 0 :(得分:5)

事情实际上比您发布的链接中的代码更简单。只需选择要映像的单元格范围,然后运行以下代码。

 Sub ExportSelection()

    If TypeName(Selection) = "Range" Then
        'Copy the area that you have selected as a picture.
        Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
        'Create a default bar(column) chart using the selected cells.
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlColumnClustered
        'Remove all the data from the chart, leaving a blank chart.
        Dim i As Integer
        For i = ActiveChart.SeriesCollection.Count To 1 Step -1
            ActiveChart.SeriesCollection(i).Delete
        Next
        'Paste the image of the selected cells onto the chart.
        ActiveChart.Paste
        'Export the chart as a gif image.
        ActiveChart.Export Environ("USERPROFILE") & "\Desktop\chart.gif"
        'Delete the existing chart.
        ActiveChart.Parent.Delete
    End If

End Sub

关键部分是ActiveChart.Export

这已在Excel 2010中经过测试,效果非常好。