简体   繁体   English

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

[英]Getting error trying to save GIF screenshot via Excel VBA macro

I am using below link to take screenshot of excel and save as .gif file: 我使用下面的链接获取excel的截图并保存为.gif文件:

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

When I try to run the macro, it gives following error at "containerbok.Activate": 当我尝试运行宏时,它在“containerbok.Activate”处给出以下错误:

Run time error '424': Object Required 运行时错误'424':对象必需

May I know why I am getting this error? 我可以知道为什么会收到此错误吗?

I am using Excel 2010 我正在使用Excel 2010

Thanks! 谢谢!

Things are actually a little simpler than the code in the link that you posted. 事情实际上比您发布的链接中的代码更简单。 Just select the range of cells you want to image, and then run the following code. 只需选择要映像的单元格范围,然后运行以下代码。

 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

The key piece is ActiveChart.Export 关键部分是ActiveChart.Export

This has been tested in Excel 2010 and works perfectly. 这已经在Excel 2010中进行了测试,并且运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM