3

我正在使用以下链接截取 excel 屏幕截图并保存为 .gif 文件:

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

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

运行时错误“424”:需要对象

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

我正在使用 Excel 2010

谢谢!

4

1 回答 1

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 中进行了测试,并且可以完美运行。

于 2013-01-02T16:56:05.383 回答