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

试图通过Excel VBA宏保存GIF屏幕截图时出错,vba,excel,excel-2007,Vba,Excel,Excel 2007,我正在使用以下链接拍摄excel屏幕截图并另存为.gif文件: 当我尝试运行宏时,它在“ContainerBook.Activate”处出现以下错误: 运行时错误“424”:需要对象 我可以知道为什么会出现这个错误吗 我正在使用Excel 2010 谢谢 事情实际上比您发布的链接中的代码要简单一些。只需选择要成像的单元格范围,然后运行以下代码 Sub ExportSelection() If TypeName(Selection) = "Range" Then 'C

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

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

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

我可以知道为什么会出现这个错误吗

我正在使用Excel 2010


谢谢

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

 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中进行了测试,效果非常好。

对我来说效果非常好(Excel 2003)。您能试用Excel 2010吗?我想。。。但仅2003年可用:-(我无法在2010年使其工作。我想他们更改了图表导出功能。感谢您的回答。但作为一名新手,我不明白您所写的内容将如何影响该链接中的代码。我必须删除“containerbook.Activate”吗。我的意思是,我应该编写什么代码来避免出现错误?请您发布修改后的代码。我非常感谢您的帮助。您需要的只是Sub和End Sub来完成代码。这段代码是Bob发布的,而其他链接中没有任何内容。我添加了Sub和End Sub,一个If语句以确保选择范围,并移动了它导出到桌面(因为我在Win 7中试图保存到根目录时遇到权限错误)。正如2010年为我所宣传的那样工作-干得好