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

mee*_*tpd 3 excel vba excel-2007 excel-vba

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

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

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

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

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

我正在使用Excel 2010

谢谢!

Ste*_*bob 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
Run Code Online (Sandbox Code Playgroud)

关键是 ActiveChart.Export

这已经在Excel 2010中进行了测试,并且运行良好.

  • 您只需要Sub和End Sub即可完成代码.这个代码是Stewbob发布的,没有其他链接.我添加了Sub和End Sub,一个If语句以确保选择是一个范围,并将导出移动到桌面(因为我在尝试保存到Win 7中的根时遇到了权限错误).像2010年为我宣传的那样 - 好工作Stewbob (3认同)